diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-06-04 11:06:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-04 11:06:42 +0200 |
| commit | 8c4c698efb7d396d5821c0328366c709461c2fe9 (patch) | |
| tree | d00fdfbcab3e05125926e16f7bdfd88b491029a5 | |
| parent | 9917f3816adce6089c90138eaf7d61213354e5fc (diff) | |
| parent | 4c6a6bc3f9466c35a749ccc25c54bb87a5951769 (diff) | |
| download | rust-8c4c698efb7d396d5821c0328366c709461c2fe9.tar.gz rust-8c4c698efb7d396d5821c0328366c709461c2fe9.zip | |
Rollup merge of #97722 - compiler-errors:tighten-copy-type-error-spans, r=Dylan-DPC
Tighten spans for bad fields in struct deriving `Copy` r? `@estebank` Closes #89137 for good, I think Not sure if this is what you were looking for in https://github.com/rust-lang/rust/issues/89137#issuecomment-1146201791
5 files changed, 15 insertions, 11 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/misc.rs b/compiler/rustc_trait_selection/src/traits/misc.rs index b83b0bf1ca5..f04f527ccb7 100644 --- a/compiler/rustc_trait_selection/src/traits/misc.rs +++ b/compiler/rustc_trait_selection/src/traits/misc.rs @@ -20,7 +20,7 @@ pub fn can_type_implement_copy<'tcx>( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, self_type: Ty<'tcx>, - cause: ObligationCause<'tcx>, + parent_cause: ObligationCause<'tcx>, ) -> Result<(), CopyImplementationError<'tcx>> { // FIXME: (@jroesch) float this code up tcx.infer_ctxt().enter(|infcx| { @@ -59,7 +59,7 @@ pub fn can_type_implement_copy<'tcx>( .ty(tcx, traits::InternalSubsts::identity_for_item(tcx, adt.did())) .has_param_types_or_consts() { - cause.clone() + parent_cause.clone() } else { ObligationCause::dummy_with_span(span) }; diff --git a/compiler/rustc_typeck/src/coherence/builtin.rs b/compiler/rustc_typeck/src/coherence/builtin.rs index c809b8bdd73..9f4e6a46d73 100644 --- a/compiler/rustc_typeck/src/coherence/builtin.rs +++ b/compiler/rustc_typeck/src/coherence/builtin.rs @@ -107,6 +107,10 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { for (field, ty) in fields { let field_span = tcx.def_span(field.did); + let field_ty_span = match tcx.hir().get_if_local(field.did) { + Some(hir::Node::Field(field_def)) => field_def.ty.span, + _ => field_span, + }; err.span_label(field_span, "this field does not implement `Copy`"); // Spin up a new FulfillmentContext, so we can get the _precise_ reason // why this field does not implement Copy. This is useful because sometimes @@ -119,7 +123,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { param_env, ty, tcx.lang_items().copy_trait().unwrap(), - traits::ObligationCause::dummy_with_span(field_span), + traits::ObligationCause::dummy_with_span(field_ty_span), ); for error in fulfill_cx.select_all_or_error(&infcx) { let error_predicate = error.obligation.predicate; diff --git a/src/test/ui/suggestions/missing-bound-in-derive-copy-impl-3.stderr b/src/test/ui/suggestions/missing-bound-in-derive-copy-impl-3.stderr index 4eb1e318d97..faf730a5ce3 100644 --- a/src/test/ui/suggestions/missing-bound-in-derive-copy-impl-3.stderr +++ b/src/test/ui/suggestions/missing-bound-in-derive-copy-impl-3.stderr @@ -10,12 +10,12 @@ LL | pub size: Vector2<K> | -------------------- this field does not implement `Copy` | note: the `Copy` impl for `Vector2<K>` requires that `K: Debug` - --> $DIR/missing-bound-in-derive-copy-impl-3.rs:12:5 + --> $DIR/missing-bound-in-derive-copy-impl-3.rs:12:14 | LL | pub loc: Vector2<K>, - | ^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^ LL | pub size: Vector2<K> - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^ = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider further restricting this bound | diff --git a/src/test/ui/suggestions/missing-bound-in-derive-copy-impl.stderr b/src/test/ui/suggestions/missing-bound-in-derive-copy-impl.stderr index 1cf2ab95bc3..11bc5409917 100644 --- a/src/test/ui/suggestions/missing-bound-in-derive-copy-impl.stderr +++ b/src/test/ui/suggestions/missing-bound-in-derive-copy-impl.stderr @@ -10,12 +10,12 @@ LL | pub size: Vector2<K> | -------------------- this field does not implement `Copy` | note: the `Copy` impl for `Vector2<K>` requires that `K: Debug` - --> $DIR/missing-bound-in-derive-copy-impl.rs:11:5 + --> $DIR/missing-bound-in-derive-copy-impl.rs:11:14 | LL | pub loc: Vector2<K>, - | ^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^ LL | pub size: Vector2<K> - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^ = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider restricting type parameter `K` | diff --git a/src/test/ui/union/union-copy.stderr b/src/test/ui/union/union-copy.stderr index 279808dd55b..8ecdafdde20 100644 --- a/src/test/ui/union/union-copy.stderr +++ b/src/test/ui/union/union-copy.stderr @@ -8,10 +8,10 @@ LL | impl Copy for W {} | ^^^^ | note: the `Copy` impl for `ManuallyDrop<String>` requires that `String: Copy` - --> $DIR/union-copy.rs:8:5 + --> $DIR/union-copy.rs:8:8 | LL | a: std::mem::ManuallyDrop<String> - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error |
