From 0ebd6e489150ce79bee339ff713078bf6eff2eae Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Tue, 20 Jul 2021 10:56:33 -0500 Subject: [PATCH] Extend HIR WF checking to fields --- compiler/rustc_typeck/src/check/wfcheck.rs | 9 +++++---- compiler/rustc_typeck/src/hir_wf_check.rs | 3 ++- src/test/ui/issues/issue-19380.stderr | 4 ++-- src/test/ui/wf/wf-in-fn-type-arg.stderr | 4 ++-- src/test/ui/wf/wf-in-fn-type-ret.stderr | 4 ++-- src/test/ui/wf/wf-in-obj-type-trait.stderr | 4 ++-- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index 98980c65bc815..e33cc603b5e54 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -523,8 +523,7 @@ fn check_type_defn<'tcx, F>( fcx.register_wf_obligation( field.ty.into(), field.span, - // We don't have an HIR id for the field - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))), ) } @@ -1467,6 +1466,7 @@ struct AdtVariant<'tcx> { struct AdtField<'tcx> { ty: Ty<'tcx>, + def_id: LocalDefId, span: Span, } @@ -1477,11 +1477,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .fields() .iter() .map(|field| { - let field_ty = self.tcx.type_of(self.tcx.hir().local_def_id(field.hir_id)); + let def_id = self.tcx.hir().local_def_id(field.hir_id); + let field_ty = self.tcx.type_of(def_id); let field_ty = self.normalize_associated_types_in(field.ty.span, field_ty); let field_ty = self.resolve_vars_if_possible(field_ty); debug!("non_enum_variant: type of field {:?} is {:?}", field, field_ty); - AdtField { ty: field_ty, span: field.ty.span } + AdtField { ty: field_ty, span: field.ty.span, def_id } }) .collect(); AdtVariant { fields, explicit_discr: None } diff --git a/compiler/rustc_typeck/src/hir_wf_check.rs b/compiler/rustc_typeck/src/hir_wf_check.rs index c1af10f5ce451..e7503d3d71cea 100644 --- a/compiler/rustc_typeck/src/hir_wf_check.rs +++ b/compiler/rustc_typeck/src/hir_wf_check.rs @@ -25,7 +25,7 @@ fn diagnostic_hir_wf_check<'tcx>( WellFormedLoc::Ty(def_id) => def_id, WellFormedLoc::Param { function, param_idx: _ } => function, }; - let hir_id = HirId::make_owner(def_id); + let hir_id = hir.local_def_id_to_hir_id(def_id); // HIR wfcheck should only ever happen as part of improving an existing error tcx.sess @@ -140,6 +140,7 @@ fn diagnostic_hir_wf_check<'tcx>( } ref item => bug!("Unexpected item {:?}", item), }, + hir::Node::Field(field) => Some(field.ty), ref node => bug!("Unexpected node {:?}", node), }, WellFormedLoc::Param { function: _, param_idx } => { diff --git a/src/test/ui/issues/issue-19380.stderr b/src/test/ui/issues/issue-19380.stderr index c3a5d3dfeee7d..4400b6179c6f8 100644 --- a/src/test/ui/issues/issue-19380.stderr +++ b/src/test/ui/issues/issue-19380.stderr @@ -1,8 +1,8 @@ error[E0038]: the trait `Qiz` cannot be made into an object - --> $DIR/issue-19380.rs:11:9 + --> $DIR/issue-19380.rs:11:29 | LL | foos: &'static [&'static (dyn Qiz + 'static)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Qiz` cannot be made into an object + | ^^^^^^^^^^^^^^^^^ `Qiz` cannot be made into an object | 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 --> $DIR/issue-19380.rs:2:6 diff --git a/src/test/ui/wf/wf-in-fn-type-arg.stderr b/src/test/ui/wf/wf-in-fn-type-arg.stderr index 33300b3964258..20aa97707105e 100644 --- a/src/test/ui/wf/wf-in-fn-type-arg.stderr +++ b/src/test/ui/wf/wf-in-fn-type-arg.stderr @@ -1,11 +1,11 @@ error[E0277]: the trait bound `T: Copy` is not satisfied - --> $DIR/wf-in-fn-type-arg.rs:9:8 + --> $DIR/wf-in-fn-type-arg.rs:9:11 | LL | struct MustBeCopy { | ---- required by this bound in `MustBeCopy` ... LL | x: fn(MustBeCopy) - | ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` + | ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` | help: consider restricting type parameter `T` | diff --git a/src/test/ui/wf/wf-in-fn-type-ret.stderr b/src/test/ui/wf/wf-in-fn-type-ret.stderr index 1ffc47e6d826c..48af696c3a752 100644 --- a/src/test/ui/wf/wf-in-fn-type-ret.stderr +++ b/src/test/ui/wf/wf-in-fn-type-ret.stderr @@ -1,11 +1,11 @@ error[E0277]: the trait bound `T: Copy` is not satisfied - --> $DIR/wf-in-fn-type-ret.rs:9:8 + --> $DIR/wf-in-fn-type-ret.rs:9:16 | LL | struct MustBeCopy { | ---- required by this bound in `MustBeCopy` ... LL | x: fn() -> MustBeCopy - | ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` + | ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` | help: consider restricting type parameter `T` | diff --git a/src/test/ui/wf/wf-in-obj-type-trait.stderr b/src/test/ui/wf/wf-in-obj-type-trait.stderr index 8606eabf59c14..b3b919a569ed4 100644 --- a/src/test/ui/wf/wf-in-obj-type-trait.stderr +++ b/src/test/ui/wf/wf-in-obj-type-trait.stderr @@ -1,11 +1,11 @@ error[E0277]: the trait bound `T: Copy` is not satisfied - --> $DIR/wf-in-obj-type-trait.rs:11:8 + --> $DIR/wf-in-obj-type-trait.rs:11:19 | LL | struct MustBeCopy { | ---- required by this bound in `MustBeCopy` ... LL | x: dyn Object> - | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` + | ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T` | help: consider restricting type parameter `T` |