Skip to content

Commit 56faafb

Browse files
committed
Add correct span for unsized types
1 parent 1b0bc59 commit 56faafb

File tree

49 files changed

+230
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+230
-184
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
269269
(err_msg, None)
270270
};
271271

272+
if tcx.is_lang_item(leaf_trait_predicate.def_id(), LangItem::Sized) {
273+
if let Some(refined_span) = self.report_sized_item(span, leaf_trait_predicate) {
274+
span = refined_span;
275+
}
276+
}
277+
272278
let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);
273279
*err.long_ty_path() = long_ty_file;
274280

@@ -769,6 +775,48 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
769775
applied_do_not_recommend
770776
}
771777

778+
fn report_sized_item(
779+
&self,
780+
span: Span,
781+
trait_pred: ty::PolyTraitPredicate<'tcx>,
782+
) -> Option<Span> {
783+
let source_map = self.tcx.sess.source_map();
784+
let snippet = source_map.span_to_snippet(span).ok()?;
785+
786+
// Skip macro
787+
if snippet.contains('!') {
788+
return None;
789+
}
790+
791+
let self_ty = trait_pred.skip_binder().self_ty().to_string();
792+
793+
// Try to find the exact type in the source code
794+
if let Some(pos) = snippet.find(&self_ty) {
795+
let start = span.lo() + BytePos(pos as u32);
796+
let end = start + BytePos(self_ty.len() as u32);
797+
return Some(Span::new(start, end, span.ctxt(), span.parent()));
798+
}
799+
800+
// Sanity check if exact match fails
801+
let mut core_type = self_ty.as_str();
802+
while core_type.starts_with('[') && core_type.ends_with(']') {
803+
core_type = &core_type[1..core_type.len() - 1].trim();
804+
if let Some(pos) = core_type.find(';') {
805+
core_type = &core_type[..pos].trim();
806+
}
807+
}
808+
809+
// Try to find core type
810+
if core_type != self_ty && !core_type.is_empty() {
811+
if let Some(pos) = snippet.find(&core_type) {
812+
let start = span.lo() + BytePos(pos as u32);
813+
let end = start + BytePos(core_type.len() as u32);
814+
return Some(Span::new(start, end, span.ctxt(), span.parent()));
815+
}
816+
}
817+
None
818+
}
819+
772820
fn report_host_effect_error(
773821
&self,
774822
predicate: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,

tests/ui/abi/debug.generic.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,10 @@ LL | type TestAbiNeSign = (fn(i32), fn(u32));
876876
| ^^^^^^^^^^^^^^^^^^
877877

878878
error[E0277]: the size for values of type `str` cannot be known at compilation time
879-
--> $DIR/debug.rs:68:46
879+
--> $DIR/debug.rs:68:47
880880
|
881881
LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
882-
| ^^^^^^^^^^ doesn't have a size known at compile-time
882+
| ^^^ doesn't have a size known at compile-time
883883
|
884884
= help: the trait `Sized` is not implemented for `str`
885885
= note: only the last element of a tuple may have a dynamically sized type

tests/ui/abi/debug.riscv64.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,10 @@ LL | type TestAbiNeSign = (fn(i32), fn(u32));
876876
| ^^^^^^^^^^^^^^^^^^
877877

878878
error[E0277]: the size for values of type `str` cannot be known at compilation time
879-
--> $DIR/debug.rs:68:46
879+
--> $DIR/debug.rs:68:47
880880
|
881881
LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
882-
| ^^^^^^^^^^ doesn't have a size known at compile-time
882+
| ^^^ doesn't have a size known at compile-time
883883
|
884884
= help: the trait `Sized` is not implemented for `str`
885885
= note: only the last element of a tuple may have a dynamically sized type

tests/ui/associated-types/defaults-wf.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2-
--> $DIR/defaults-wf.rs:7:15
2+
--> $DIR/defaults-wf.rs:7:19
33
|
44
LL | type Ty = Vec<[u8]>;
5-
| ^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
88
note: required by an implicit `Sized` bound in `Vec`

tests/ui/associated-types/issue-20005.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `Self` cannot be known at compilation time
2-
--> $DIR/issue-20005.rs:10:49
2+
--> $DIR/issue-20005.rs:10:54
33
|
44
LL | ) -> <Dst as From<Self>>::Result where Dst: From<Self> {
5-
| ^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^ doesn't have a size known at compile-time
66
|
77
note: required by an implicit `Sized` bound in `From`
88
--> $DIR/issue-20005.rs:1:12

tests/ui/associated-types/trait-with-supertraits-needing-sized-self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `Self` cannot be known at compilation time
2-
--> $DIR/trait-with-supertraits-needing-sized-self.rs:3:22
2+
--> $DIR/trait-with-supertraits-needing-sized-self.rs:3:33
33
|
44
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> {}
5-
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^ doesn't have a size known at compile-time
66
|
77
note: required by an implicit `Sized` bound in `Add`
88
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL

tests/ui/cast/unsized-union-ice.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2-
--> $DIR/unsized-union-ice.rs:6:10
2+
--> $DIR/unsized-union-ice.rs:6:33
33
|
44
LL | val: std::mem::ManuallyDrop<[u8]>,
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^ doesn't have a size known at compile-time
66
|
77
= help: within `ManuallyDrop<[u8]>`, the trait `Sized` is not implemented for `[u8]`
88
note: required because it appears within the type `ManuallyDrop<[u8]>`

tests/ui/closures/closure-return-type-must-be-sized.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
2-
--> $DIR/closure-return-type-must-be-sized.rs:54:5
2+
--> $DIR/closure-return-type-must-be-sized.rs:54:22
33
|
44
LL | a::foo::<fn() -> dyn A>();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^^ doesn't have a size known at compile-time
66
|
77
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
88
= note: required because it appears within the type `fn() -> dyn A`
99

1010
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
11-
--> $DIR/closure-return-type-must-be-sized.rs:55:14
11+
--> $DIR/closure-return-type-must-be-sized.rs:55:22
1212
|
1313
LL | a::bar::<fn() -> dyn A, _>();
14-
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
14+
| ^^^^^ doesn't have a size known at compile-time
1515
|
1616
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
1717
= note: required because it appears within the type `fn() -> dyn A`
@@ -22,28 +22,28 @@ LL | pub fn bar<F: FnOnce() -> R, R: ?Sized>() {}
2222
| ^^^^^^^^^^^^^ required by this bound in `bar`
2323

2424
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
25-
--> $DIR/closure-return-type-must-be-sized.rs:56:5
25+
--> $DIR/closure-return-type-must-be-sized.rs:56:22
2626
|
2727
LL | a::baz::<fn() -> dyn A>();
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
28+
| ^^^^^ doesn't have a size known at compile-time
2929
|
3030
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
3131
= note: required because it appears within the type `fn() -> dyn A`
3232

3333
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
34-
--> $DIR/closure-return-type-must-be-sized.rs:61:5
34+
--> $DIR/closure-return-type-must-be-sized.rs:61:22
3535
|
3636
LL | b::foo::<fn() -> dyn A>();
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
37+
| ^^^^^ doesn't have a size known at compile-time
3838
|
3939
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
4040
= note: required because it appears within the type `fn() -> dyn A`
4141

4242
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
43-
--> $DIR/closure-return-type-must-be-sized.rs:62:14
43+
--> $DIR/closure-return-type-must-be-sized.rs:62:22
4444
|
4545
LL | b::bar::<fn() -> dyn A, _>();
46-
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
46+
| ^^^^^ doesn't have a size known at compile-time
4747
|
4848
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
4949
= note: required because it appears within the type `fn() -> dyn A`
@@ -54,28 +54,28 @@ LL | pub fn bar<F: Fn() -> R, R: ?Sized>() {}
5454
| ^^^^^^^^^ required by this bound in `bar`
5555

5656
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
57-
--> $DIR/closure-return-type-must-be-sized.rs:63:5
57+
--> $DIR/closure-return-type-must-be-sized.rs:63:22
5858
|
5959
LL | b::baz::<fn() -> dyn A>();
60-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
60+
| ^^^^^ doesn't have a size known at compile-time
6161
|
6262
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
6363
= note: required because it appears within the type `fn() -> dyn A`
6464

6565
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
66-
--> $DIR/closure-return-type-must-be-sized.rs:68:5
66+
--> $DIR/closure-return-type-must-be-sized.rs:68:22
6767
|
6868
LL | c::foo::<fn() -> dyn A>();
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
69+
| ^^^^^ doesn't have a size known at compile-time
7070
|
7171
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
7272
= note: required because it appears within the type `fn() -> dyn A`
7373

7474
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
75-
--> $DIR/closure-return-type-must-be-sized.rs:69:14
75+
--> $DIR/closure-return-type-must-be-sized.rs:69:22
7676
|
7777
LL | c::bar::<fn() -> dyn A, _>();
78-
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
78+
| ^^^^^ doesn't have a size known at compile-time
7979
|
8080
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
8181
= note: required because it appears within the type `fn() -> dyn A`
@@ -86,10 +86,10 @@ LL | pub fn bar<F: FnMut() -> R, R: ?Sized>() {}
8686
| ^^^^^^^^^^^^ required by this bound in `bar`
8787

8888
error[E0277]: the size for values of type `dyn A` cannot be known at compilation time
89-
--> $DIR/closure-return-type-must-be-sized.rs:70:5
89+
--> $DIR/closure-return-type-must-be-sized.rs:70:22
9090
|
9191
LL | c::baz::<fn() -> dyn A>();
92-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
92+
| ^^^^^ doesn't have a size known at compile-time
9393
|
9494
= help: within `fn() -> dyn A`, the trait `Sized` is not implemented for `dyn A`
9595
= note: required because it appears within the type `fn() -> dyn A`

tests/ui/closures/missing-body.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | fn main() { |b: [str; _]| {}; }
55
| ^ not allowed in type signatures
66

77
error[E0277]: the size for values of type `str` cannot be known at compilation time
8-
--> $DIR/missing-body.rs:5:17
8+
--> $DIR/missing-body.rs:5:18
99
|
1010
LL | fn main() { |b: [str; _]| {}; }
11-
| ^^^^^^^^ doesn't have a size known at compile-time
11+
| ^^^ doesn't have a size known at compile-time
1212
|
1313
= help: the trait `Sized` is not implemented for `str`
1414
= note: slice and array elements must have `Sized` type

tests/ui/consts/const-unsized.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
2626
= note: statics and constants must have a statically known size
2727

2828
error[E0277]: the size for values of type `str` cannot be known at compilation time
29-
--> $DIR/const-unsized.rs:15:1
29+
--> $DIR/const-unsized.rs:15:20
3030
|
3131
LL | static STATIC_BAR: str = *"bar";
32-
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
32+
| ^^^ doesn't have a size known at compile-time
3333
|
3434
= help: the trait `Sized` is not implemented for `str`
3535
= note: statics and constants must have a statically known size

0 commit comments

Comments
 (0)