Skip to content

Commit 7d5f3b2

Browse files
committed
Add more tracking for the HIR source of well-formedness requirement
1 parent 5d9b514 commit 7d5f3b2

File tree

10 files changed

+58
-12
lines changed

10 files changed

+58
-12
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ fn check_item_type(
12701270
traits::ObligationCause::new(
12711271
ty_span,
12721272
wfcx.body_def_id,
1273-
ObligationCauseCode::WellFormed(None),
1273+
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(item_id))),
12741274
),
12751275
wfcx.param_env,
12761276
item_ty,

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn diagnostic_hir_wf_check<'tcx>(
2525
let def_id = match loc {
2626
WellFormedLoc::Ty(def_id) => def_id,
2727
WellFormedLoc::Param { function, param_idx: _ } => function,
28+
WellFormedLoc::Expr(_) => return None,
2829
};
2930
let hir_id = tcx.local_def_id_to_hir_id(def_id);
3031

@@ -79,7 +80,9 @@ fn diagnostic_hir_wf_check<'tcx>(
7980
let cause = traits::ObligationCause::new(
8081
ty.span,
8182
self.def_id,
82-
traits::ObligationCauseCode::WellFormed(None),
83+
traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(
84+
ty.hir_id,
85+
))),
8386
);
8487

8588
ocx.register_obligation(traits::Obligation::new(
@@ -191,6 +194,7 @@ fn diagnostic_hir_wf_check<'tcx>(
191194
vec![&fn_decl.inputs[param_idx as usize]]
192195
}
193196
}
197+
WellFormedLoc::Expr(_) => return None,
194198
};
195199
for ty in tys {
196200
visitor.visit_ty(ty);

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
122122
self.register_wf_obligation(
123123
output.into(),
124124
call_expr.span,
125-
ObligationCauseCode::WellFormed(None),
125+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(call_expr.hir_id))),
126126
);
127127

128128
output

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15001500

15011501
let ty = Ty::new_array_with_const_len(tcx, t, count);
15021502

1503-
self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1503+
self.register_wf_obligation(
1504+
ty.into(),
1505+
expr.span,
1506+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(expr.hir_id))),
1507+
);
15041508

15051509
ty
15061510
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
405405

406406
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> LoweredTy<'tcx> {
407407
let ty = self.lowerer().lower_ty(hir_ty);
408-
self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None));
408+
self.register_wf_obligation(
409+
ty.into(),
410+
hir_ty.span,
411+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_ty.hir_id))),
412+
);
409413
LoweredTy::from_raw(self, hir_ty.span, ty)
410414
}
411415

@@ -516,7 +520,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
516520
for arg in args.iter().filter(|arg| {
517521
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
518522
}) {
519-
self.register_wf_obligation(arg, expr.span, ObligationCauseCode::WellFormed(None));
523+
self.register_wf_obligation(
524+
arg,
525+
expr.span,
526+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(expr.hir_id))),
527+
);
520528
}
521529
}
522530

@@ -771,7 +779,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
771779
self.register_wf_obligation(
772780
ty.raw.into(),
773781
qself.span,
774-
ObligationCauseCode::WellFormed(None),
782+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
775783
);
776784
// Return directly on cache hit. This is useful to avoid doubly reporting
777785
// errors with default match binding modes. See #44614.
@@ -811,7 +819,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
811819
self.register_wf_obligation(
812820
ty.raw.into(),
813821
qself.span,
814-
ObligationCauseCode::WellFormed(None),
822+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
815823
);
816824
}
817825

@@ -849,7 +857,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
849857
self.register_wf_obligation(
850858
ty.raw.into(),
851859
qself.span,
852-
ObligationCauseCode::WellFormed(None),
860+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
853861
);
854862
}
855863

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,13 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
586586
// the function type must also be well-formed (this is not
587587
// implied by the args being well-formed because of inherent
588588
// impls and late-bound regions - see issue #28609).
589-
self.register_wf_obligation(fty.into(), self.span, ObligationCauseCode::WellFormed(None));
589+
self.register_wf_obligation(
590+
fty.into(),
591+
self.span,
592+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(
593+
self.call_expr.hir_id,
594+
))),
595+
);
590596
}
591597

592598
///////////////////////////////////////////////////////////////////////////

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ pub enum WellFormedLoc {
486486
/// being the last 'parameter'
487487
param_idx: usize,
488488
},
489+
/// The HIR node for the expression that introduced this obligation.
490+
Expr(HirId),
489491
}
490492

491493
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
871871
}
872872

873873
ty::PredicateKind::ObjectSafe(trait_def_id) => {
874+
let mut hir_id = None;
875+
if let ObligationCauseCode::WellFormed(Some(
876+
crate::traits::WellFormedLoc::Expr(id),
877+
)) = root_obligation.cause.code() {
878+
hir_id = Some(*id);
879+
}
874880
let violations = self.tcx.object_safety_violations(trait_def_id);
875-
report_object_safety_error(self.tcx, span, None, trait_def_id, violations)
881+
report_object_safety_error(self.tcx, span, hir_id, trait_def_id, violations)
876882
}
877883

878884
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty)) => {

tests/ui/consts/const_refs_to_static-ice-121413.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ const REF_INTERIOR_MUT: &usize = {
88
static FOO: Sync = AtomicUsize::new(0);
99
//~^ ERROR failed to resolve: use of undeclared type `AtomicUsize`
1010
//~| WARN trait objects without an explicit `dyn` are deprecated
11+
//~| WARN trait objects without an explicit `dyn` are deprecated
1112
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
1213
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
1314
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
15+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
1416
unsafe { &*(&FOO as *const _ as *const usize) }
1517
};
1618
pub fn main() {}

tests/ui/consts/const_refs_to_static-ice-121413.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ help: if this is an object-safe trait, use `dyn`
2323
LL | static FOO: dyn Sync = AtomicUsize::new(0);
2424
| +++
2525

26+
warning: trait objects without an explicit `dyn` are deprecated
27+
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
28+
|
29+
LL | static FOO: Sync = AtomicUsize::new(0);
30+
| ^^^^
31+
|
32+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
33+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
34+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
35+
help: if this is an object-safe trait, use `dyn`
36+
|
37+
LL | static FOO: dyn Sync = AtomicUsize::new(0);
38+
| +++
39+
2640
error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
2741
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
2842
|
@@ -40,7 +54,7 @@ LL | static FOO: Sync = AtomicUsize::new(0);
4054
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
4155
= note: constant expressions must have a statically known size
4256

43-
error: aborting due to 3 previous errors; 1 warning emitted
57+
error: aborting due to 3 previous errors; 2 warnings emitted
4458

4559
Some errors have detailed explanations: E0277, E0433.
4660
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)