Skip to content

Commit 5441f71

Browse files
committed
Add more tracking for the HIR source of well-formedness requirement
1 parent f1f9842 commit 5441f71

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
@@ -1517,7 +1517,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15171517

15181518
let ty = Ty::new_array_with_const_len(tcx, t, count);
15191519

1520-
self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1520+
self.register_wf_obligation(
1521+
ty.into(),
1522+
expr.span,
1523+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(expr.hir_id))),
1524+
);
15211525

15221526
ty
15231527
}

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

@@ -770,7 +778,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
770778
self.register_wf_obligation(
771779
ty.raw.into(),
772780
qself.span,
773-
ObligationCauseCode::WellFormed(None),
781+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
774782
);
775783
// Return directly on cache hit. This is useful to avoid doubly reporting
776784
// errors with default match binding modes. See #44614.
@@ -810,7 +818,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
810818
self.register_wf_obligation(
811819
ty.raw.into(),
812820
qself.span,
813-
ObligationCauseCode::WellFormed(None),
821+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
814822
);
815823
}
816824

@@ -844,7 +852,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
844852
self.register_wf_obligation(
845853
ty.raw.into(),
846854
qself.span,
847-
ObligationCauseCode::WellFormed(None),
855+
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
848856
);
849857
}
850858

compiler/rustc_hir_typeck/src/method/confirm.rs

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

589595
///////////////////////////////////////////////////////////////////////////

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
@@ -870,8 +870,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
870870
}
871871

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

877883
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)