Skip to content

Commit 8c4c698

Browse files
authored
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 #89137 (comment)
2 parents 9917f38 + 4c6a6bc commit 8c4c698

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

compiler/rustc_trait_selection/src/traits/misc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn can_type_implement_copy<'tcx>(
2020
tcx: TyCtxt<'tcx>,
2121
param_env: ty::ParamEnv<'tcx>,
2222
self_type: Ty<'tcx>,
23-
cause: ObligationCause<'tcx>,
23+
parent_cause: ObligationCause<'tcx>,
2424
) -> Result<(), CopyImplementationError<'tcx>> {
2525
// FIXME: (@jroesch) float this code up
2626
tcx.infer_ctxt().enter(|infcx| {
@@ -59,7 +59,7 @@ pub fn can_type_implement_copy<'tcx>(
5959
.ty(tcx, traits::InternalSubsts::identity_for_item(tcx, adt.did()))
6060
.has_param_types_or_consts()
6161
{
62-
cause.clone()
62+
parent_cause.clone()
6363
} else {
6464
ObligationCause::dummy_with_span(span)
6565
};

compiler/rustc_typeck/src/coherence/builtin.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
107107

108108
for (field, ty) in fields {
109109
let field_span = tcx.def_span(field.did);
110+
let field_ty_span = match tcx.hir().get_if_local(field.did) {
111+
Some(hir::Node::Field(field_def)) => field_def.ty.span,
112+
_ => field_span,
113+
};
110114
err.span_label(field_span, "this field does not implement `Copy`");
111115
// Spin up a new FulfillmentContext, so we can get the _precise_ reason
112116
// 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) {
119123
param_env,
120124
ty,
121125
tcx.lang_items().copy_trait().unwrap(),
122-
traits::ObligationCause::dummy_with_span(field_span),
126+
traits::ObligationCause::dummy_with_span(field_ty_span),
123127
);
124128
for error in fulfill_cx.select_all_or_error(&infcx) {
125129
let error_predicate = error.obligation.predicate;

src/test/ui/suggestions/missing-bound-in-derive-copy-impl-3.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ LL | pub size: Vector2<K>
1010
| -------------------- this field does not implement `Copy`
1111
|
1212
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
13-
--> $DIR/missing-bound-in-derive-copy-impl-3.rs:12:5
13+
--> $DIR/missing-bound-in-derive-copy-impl-3.rs:12:14
1414
|
1515
LL | pub loc: Vector2<K>,
16-
| ^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^^^^^^
1717
LL | pub size: Vector2<K>
18-
| ^^^^^^^^^^^^^^^^^^^^
18+
| ^^^^^^^^^^
1919
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
2020
help: consider further restricting this bound
2121
|

src/test/ui/suggestions/missing-bound-in-derive-copy-impl.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ LL | pub size: Vector2<K>
1010
| -------------------- this field does not implement `Copy`
1111
|
1212
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
13-
--> $DIR/missing-bound-in-derive-copy-impl.rs:11:5
13+
--> $DIR/missing-bound-in-derive-copy-impl.rs:11:14
1414
|
1515
LL | pub loc: Vector2<K>,
16-
| ^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^^^^^^
1717
LL | pub size: Vector2<K>
18-
| ^^^^^^^^^^^^^^^^^^^^
18+
| ^^^^^^^^^^
1919
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
2020
help: consider restricting type parameter `K`
2121
|

src/test/ui/union/union-copy.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | impl Copy for W {}
88
| ^^^^
99
|
1010
note: the `Copy` impl for `ManuallyDrop<String>` requires that `String: Copy`
11-
--> $DIR/union-copy.rs:8:5
11+
--> $DIR/union-copy.rs:8:8
1212
|
1313
LL | a: std::mem::ManuallyDrop<String>
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
error: aborting due to previous error
1717

0 commit comments

Comments
 (0)