Skip to content

Commit c7f662b

Browse files
committed
Add correct span for unsized types
1 parent c720f49 commit c7f662b

File tree

48 files changed

+228
-182
lines changed

Some content is hidden

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

48 files changed

+228
-182
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.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:64:46
879+
--> $DIR/debug.rs:64: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

tests/ui/consts/dont-ctfe-unsized-initializer.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 `str` cannot be known at compilation time
2-
--> $DIR/dont-ctfe-unsized-initializer.rs:1:1
2+
--> $DIR/dont-ctfe-unsized-initializer.rs:1:11
33
|
44
LL | static S: str = todo!();
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 `str`
88
= note: statics and constants must have a statically known size

tests/ui/dst/dst-sized-trait-param.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 `[isize]` cannot be known at compilation time
2-
--> $DIR/dst-sized-trait-param.rs:7:6
2+
--> $DIR/dst-sized-trait-param.rs:7:10
33
|
44
LL | impl Foo<[isize]> for usize { }
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 `[isize]`
88
note: required by an implicit `Sized` bound in `Foo`

tests/ui/dyn-compatibility/supertrait-mentions-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/supertrait-mentions-Self.rs:8:13
2+
--> $DIR/supertrait-mentions-Self.rs:8:17
33
|
44
LL | trait Baz : Bar<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 `Bar`
88
--> $DIR/supertrait-mentions-Self.rs:4:11

tests/ui/extern/extern-types-unsized.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ LL | fn assert_sized<T: ?Sized>() {}
3838
| ++++++++
3939

4040
error[E0277]: the size for values of type `A` cannot be known at compilation time
41-
--> $DIR/extern-types-unsized.rs:28:20
41+
--> $DIR/extern-types-unsized.rs:28:24
4242
|
4343
LL | assert_sized::<Bar<A>>();
44-
| ^^^^^^ doesn't have a size known at compile-time
44+
| ^ doesn't have a size known at compile-time
4545
|
4646
= help: within `Bar<A>`, the trait `Sized` is not implemented for `A`
4747
note: required because it appears within the type `Bar<A>`
@@ -73,10 +73,10 @@ LL | struct Bar<T: ?Sized> {
7373
| ^ required by this bound in `Bar`
7474

7575
error[E0277]: the size for values of type `A` cannot be known at compilation time
76-
--> $DIR/extern-types-unsized.rs:32:20
76+
--> $DIR/extern-types-unsized.rs:32:28
7777
|
7878
LL | assert_sized::<Bar<Bar<A>>>();
79-
| ^^^^^^^^^^^ doesn't have a size known at compile-time
79+
| ^ doesn't have a size known at compile-time
8080
|
8181
= help: within `Bar<Bar<A>>`, the trait `Sized` is not implemented for `A`
8282
note: required because it appears within the type `Bar<A>`

tests/ui/extern/issue-36122-accessing-externed-dst.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 `[usize]` cannot be known at compilation time
2-
--> $DIR/issue-36122-accessing-externed-dst.rs:3:9
2+
--> $DIR/issue-36122-accessing-externed-dst.rs:3:24
33
|
44
LL | static symbol: [usize];
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 `[usize]`
88
= note: statics and constants must have a statically known size

tests/ui/feature-gates/feature-gate-trivial_bounds.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
105105
--> $DIR/feature-gate-trivial_bounds.rs:52:32
106106
|
107107
LL | struct TwoStrs(str, str) where str: Sized;
108-
| ^^^^^^^^^^ doesn't have a size known at compile-time
108+
| ^^^ doesn't have a size known at compile-time
109109
|
110110
= help: the trait `Sized` is not implemented for `str`
111111
= help: see issue #48214
@@ -136,7 +136,7 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
136136
--> $DIR/feature-gate-trivial_bounds.rs:59:30
137137
|
138138
LL | fn return_str() -> str where str: Sized {
139-
| ^^^^^^^^^^ doesn't have a size known at compile-time
139+
| ^^^ doesn't have a size known at compile-time
140140
|
141141
= help: the trait `Sized` is not implemented for `str`
142142
= help: see issue #48214

tests/ui/function-pointer/unsized-ret.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `str` cannot be known at compilation time
2-
--> $DIR/unsized-ret.rs:10:11
2+
--> $DIR/unsized-ret.rs:10:19
33
|
44
LL | foo::<fn() -> str, _>(None, ());
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() -> str`, the trait `Sized` is not implemented for `str`
88
= note: required because it appears within the type `fn() -> str`
@@ -13,10 +13,10 @@ LL | fn foo<F: Fn<T>, T:std::marker::Tuple>(f: Option<F>, t: T) {
1313
| ^^^^^ required by this bound in `foo`
1414

1515
error[E0277]: the size for values of type `(dyn std::fmt::Display + 'a)` cannot be known at compilation time
16-
--> $DIR/unsized-ret.rs:13:11
16+
--> $DIR/unsized-ret.rs:13:33
1717
|
1818
LL | foo::<for<'a> fn(&'a ()) -> (dyn std::fmt::Display + 'a), _>(None, (&(),));
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2020
|
2121
= help: within `for<'a> fn(&'a ()) -> (dyn std::fmt::Display + 'a)`, the trait `for<'a> Sized` is not implemented for `(dyn std::fmt::Display + 'a)`
2222
= note: required because it appears within the type `for<'a> fn(&'a ()) -> (dyn std::fmt::Display + 'a)`

tests/ui/higher-ranked/well-formed-aliases.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 `str` cannot be known at compilation time
2-
--> $DIR/well-formed-aliases.rs:5:52
2+
--> $DIR/well-formed-aliases.rs:5:53
33
|
44
LL | fn test<T>(f: for<'a> fn(<&'a T as Trait>::Gat<&'a [str]>)) where for<'a> &'a T: Trait {}
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 `str`
88
= note: slice and array elements must have `Sized` type

0 commit comments

Comments
 (0)