Skip to content

Commit 0ebd6e4

Browse files
committed
Extend HIR WF checking to fields
1 parent f913a4f commit 0ebd6e4

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

compiler/rustc_typeck/src/check/wfcheck.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ fn check_type_defn<'tcx, F>(
523523
fcx.register_wf_obligation(
524524
field.ty.into(),
525525
field.span,
526-
// We don't have an HIR id for the field
527-
ObligationCauseCode::WellFormed(None),
526+
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
528527
)
529528
}
530529

@@ -1467,6 +1466,7 @@ struct AdtVariant<'tcx> {
14671466

14681467
struct AdtField<'tcx> {
14691468
ty: Ty<'tcx>,
1469+
def_id: LocalDefId,
14701470
span: Span,
14711471
}
14721472

@@ -1477,11 +1477,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14771477
.fields()
14781478
.iter()
14791479
.map(|field| {
1480-
let field_ty = self.tcx.type_of(self.tcx.hir().local_def_id(field.hir_id));
1480+
let def_id = self.tcx.hir().local_def_id(field.hir_id);
1481+
let field_ty = self.tcx.type_of(def_id);
14811482
let field_ty = self.normalize_associated_types_in(field.ty.span, field_ty);
14821483
let field_ty = self.resolve_vars_if_possible(field_ty);
14831484
debug!("non_enum_variant: type of field {:?} is {:?}", field, field_ty);
1484-
AdtField { ty: field_ty, span: field.ty.span }
1485+
AdtField { ty: field_ty, span: field.ty.span, def_id }
14851486
})
14861487
.collect();
14871488
AdtVariant { fields, explicit_discr: None }

compiler/rustc_typeck/src/hir_wf_check.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn diagnostic_hir_wf_check<'tcx>(
2525
WellFormedLoc::Ty(def_id) => def_id,
2626
WellFormedLoc::Param { function, param_idx: _ } => function,
2727
};
28-
let hir_id = HirId::make_owner(def_id);
28+
let hir_id = hir.local_def_id_to_hir_id(def_id);
2929

3030
// HIR wfcheck should only ever happen as part of improving an existing error
3131
tcx.sess
@@ -140,6 +140,7 @@ fn diagnostic_hir_wf_check<'tcx>(
140140
}
141141
ref item => bug!("Unexpected item {:?}", item),
142142
},
143+
hir::Node::Field(field) => Some(field.ty),
143144
ref node => bug!("Unexpected node {:?}", node),
144145
},
145146
WellFormedLoc::Param { function: _, param_idx } => {

src/test/ui/issues/issue-19380.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0038]: the trait `Qiz` cannot be made into an object
2-
--> $DIR/issue-19380.rs:11:9
2+
--> $DIR/issue-19380.rs:11:29
33
|
44
LL | foos: &'static [&'static (dyn Qiz + 'static)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Qiz` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^ `Qiz` cannot be made into an object
66
|
77
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
88
--> $DIR/issue-19380.rs:2:6

src/test/ui/wf/wf-in-fn-type-arg.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `T: Copy` is not satisfied
2-
--> $DIR/wf-in-fn-type-arg.rs:9:8
2+
--> $DIR/wf-in-fn-type-arg.rs:9:11
33
|
44
LL | struct MustBeCopy<T:Copy> {
55
| ---- required by this bound in `MustBeCopy`
66
...
77
LL | x: fn(MustBeCopy<T>)
8-
| ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
8+
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
99
|
1010
help: consider restricting type parameter `T`
1111
|

src/test/ui/wf/wf-in-fn-type-ret.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `T: Copy` is not satisfied
2-
--> $DIR/wf-in-fn-type-ret.rs:9:8
2+
--> $DIR/wf-in-fn-type-ret.rs:9:16
33
|
44
LL | struct MustBeCopy<T:Copy> {
55
| ---- required by this bound in `MustBeCopy`
66
...
77
LL | x: fn() -> MustBeCopy<T>
8-
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
8+
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
99
|
1010
help: consider restricting type parameter `T`
1111
|

src/test/ui/wf/wf-in-obj-type-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `T: Copy` is not satisfied
2-
--> $DIR/wf-in-obj-type-trait.rs:11:8
2+
--> $DIR/wf-in-obj-type-trait.rs:11:19
33
|
44
LL | struct MustBeCopy<T:Copy> {
55
| ---- required by this bound in `MustBeCopy`
66
...
77
LL | x: dyn Object<MustBeCopy<T>>
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
8+
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
99
|
1010
help: consider restricting type parameter `T`
1111
|

0 commit comments

Comments
 (0)