Skip to content

Commit af10a45

Browse files
committed
Track implicit Sized obligations in type params
Suggest adding a `?Sized` bound if appropriate on E0599 by inspecting the HIR Generics. (Fix #98539)
1 parent d5642ac commit af10a45

File tree

59 files changed

+213
-124
lines changed

Some content is hidden

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

59 files changed

+213
-124
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+39-12
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
348348
let type_param = generics.type_param(param_type, self.tcx);
349349
Some(self.tcx.def_span(type_param.def_id))
350350
}
351-
ty::Adt(def, _) if def.did().is_local() => {
352-
tcx.def_ident_span(def.did()).map(|span| span)
353-
}
351+
ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
354352
_ => None,
355353
};
356354

@@ -621,12 +619,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
621619
// Find all the requirements that come from a local `impl` block.
622620
let mut skip_list: FxHashSet<_> = Default::default();
623621
let mut spanned_predicates: FxHashMap<MultiSpan, _> = Default::default();
624-
for (data, p, parent_p, impl_def_id, cause_span) in unsatisfied_predicates
622+
for (data, p, parent_p, impl_def_id, cause) in unsatisfied_predicates
625623
.iter()
626624
.filter_map(|(p, parent, c)| c.as_ref().map(|c| (p, parent, c)))
627625
.filter_map(|(p, parent, c)| match c.code() {
628626
ObligationCauseCode::ImplDerivedObligation(ref data) => {
629-
Some((&data.derived, p, parent, data.impl_def_id, data.span))
627+
Some((&data.derived, p, parent, data.impl_def_id, data))
630628
}
631629
_ => None,
632630
})
@@ -695,9 +693,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
695693
let _ = format_pred(*pred);
696694
}
697695
skip_list.insert(p);
698-
let mut spans = if cause_span != *item_span {
699-
let mut spans: MultiSpan = cause_span.into();
700-
spans.push_span_label(cause_span, unsatisfied_msg);
696+
let mut spans = if cause.span != *item_span {
697+
let mut spans: MultiSpan = cause.span.into();
698+
spans.push_span_label(cause.span, unsatisfied_msg);
701699
spans
702700
} else {
703701
ident.span.into()
@@ -709,7 +707,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
709707

710708
// Unmet obligation coming from an `impl`.
711709
Some(Node::Item(hir::Item {
712-
kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
710+
kind:
711+
hir::ItemKind::Impl(hir::Impl {
712+
of_trait, self_ty, generics, ..
713+
}),
713714
span: item_span,
714715
..
715716
})) if !matches!(
@@ -725,14 +726,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
725726
Some(ExpnKind::Macro(MacroKind::Derive, _))
726727
) =>
727728
{
729+
let sized_pred =
730+
unsatisfied_predicates.iter().any(|(pred, _, _)| {
731+
match pred.kind().skip_binder() {
732+
ty::PredicateKind::Trait(pred) => {
733+
Some(pred.def_id())
734+
== self.tcx.lang_items().sized_trait()
735+
&& pred.polarity == ty::ImplPolarity::Positive
736+
}
737+
_ => false,
738+
}
739+
});
740+
for param in generics.params {
741+
if param.span == cause.span && sized_pred {
742+
let (sp, sugg) = match param.colon_span {
743+
Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
744+
None => (param.span.shrink_to_hi(), ": ?Sized"),
745+
};
746+
err.span_suggestion_verbose(
747+
sp,
748+
"consider relaxing the type parameter's implicit \
749+
`Sized` bound",
750+
sugg,
751+
Applicability::MachineApplicable,
752+
);
753+
}
754+
}
728755
if let Some(pred) = parent_p {
729756
// Done to add the "doesn't satisfy" `span_label`.
730757
let _ = format_pred(*pred);
731758
}
732759
skip_list.insert(p);
733-
let mut spans = if cause_span != *item_span {
734-
let mut spans: MultiSpan = cause_span.into();
735-
spans.push_span_label(cause_span, unsatisfied_msg);
760+
let mut spans = if cause.span != *item_span {
761+
let mut spans: MultiSpan = cause.span.into();
762+
spans.push_span_label(cause.span, unsatisfied_msg);
736763
spans
737764
} else {
738765
let mut spans = Vec::with_capacity(2);

src/test/ui/associated-item/associated-item-enum.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no variant or associated item named `mispellable` found for enum `
22
--> $DIR/associated-item-enum.rs:17:11
33
|
44
LL | enum Enum { Variant }
5-
| ---- variant or associated item `mispellable` not found for this enum
5+
| --------- variant or associated item `mispellable` not found for this enum
66
...
77
LL | Enum::mispellable();
88
| ^^^^^^^^^^^
@@ -14,7 +14,7 @@ error[E0599]: no variant or associated item named `mispellable_trait` found for
1414
--> $DIR/associated-item-enum.rs:18:11
1515
|
1616
LL | enum Enum { Variant }
17-
| ---- variant or associated item `mispellable_trait` not found for this enum
17+
| --------- variant or associated item `mispellable_trait` not found for this enum
1818
...
1919
LL | Enum::mispellable_trait();
2020
| ^^^^^^^^^^^^^^^^^
@@ -26,7 +26,7 @@ error[E0599]: no variant or associated item named `MISPELLABLE` found for enum `
2626
--> $DIR/associated-item-enum.rs:19:11
2727
|
2828
LL | enum Enum { Variant }
29-
| ---- variant or associated item `MISPELLABLE` not found for this enum
29+
| --------- variant or associated item `MISPELLABLE` not found for this enum
3030
...
3131
LL | Enum::MISPELLABLE;
3232
| ^^^^^^^^^^^

src/test/ui/async-await/pin-needed-to-poll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `poll` found for struct `Sleep` in the current sco
22
--> $DIR/pin-needed-to-poll.rs:42:20
33
|
44
LL | struct Sleep;
5-
| ----- method `poll` not found for this struct
5+
| ------------ method `poll` not found for this struct
66
...
77
LL | self.sleep.poll(cx)
88
| ^^^^ method not found in `Sleep`

src/test/ui/bogus-tag.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no variant or associated item named `Hsl` found for enum `Color` i
22
--> $DIR/bogus-tag.rs:7:16
33
|
44
LL | enum Color { Rgb(isize, isize, isize), Rgba(isize, isize, isize, isize), }
5-
| ----- variant or associated item `Hsl` not found for this enum
5+
| ---------- variant or associated item `Hsl` not found for this enum
66
...
77
LL | Color::Hsl(h, s, l) => { println!("hsl"); }
88
| ^^^ variant or associated item not found in `Color`

src/test/ui/confuse-field-and-method/issue-18343.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
22
--> $DIR/issue-18343.rs:7:7
33
|
44
LL | struct Obj<F> where F: FnMut() -> u32 {
5-
| --- method `closure` not found for this struct
5+
| ------------- method `closure` not found for this struct
66
...
77
LL | o.closure();
88
| ^^^^^^^ field, not a method

src/test/ui/confuse-field-and-method/issue-2392.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
22
--> $DIR/issue-2392.rs:36:15
33
|
44
LL | struct Obj<F> where F: FnOnce() -> u32 {
5-
| --- method `closure` not found for this struct
5+
| ------------- method `closure` not found for this struct
66
...
77
LL | o_closure.closure();
88
| ^^^^^^^ field, not a method
@@ -16,7 +16,7 @@ error[E0599]: no method named `not_closure` found for struct `Obj` in the curren
1616
--> $DIR/issue-2392.rs:38:15
1717
|
1818
LL | struct Obj<F> where F: FnOnce() -> u32 {
19-
| --- method `not_closure` not found for this struct
19+
| ------------- method `not_closure` not found for this struct
2020
...
2121
LL | o_closure.not_closure();
2222
| ^^^^^^^^^^^-- help: remove the arguments
@@ -27,7 +27,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
2727
--> $DIR/issue-2392.rs:42:12
2828
|
2929
LL | struct Obj<F> where F: FnOnce() -> u32 {
30-
| --- method `closure` not found for this struct
30+
| ------------- method `closure` not found for this struct
3131
...
3232
LL | o_func.closure();
3333
| ^^^^^^^ field, not a method
@@ -41,7 +41,7 @@ error[E0599]: no method named `boxed_closure` found for struct `BoxedObj` in the
4141
--> $DIR/issue-2392.rs:45:14
4242
|
4343
LL | struct BoxedObj {
44-
| -------- method `boxed_closure` not found for this struct
44+
| --------------- method `boxed_closure` not found for this struct
4545
...
4646
LL | boxed_fn.boxed_closure();
4747
| ^^^^^^^^^^^^^ field, not a method
@@ -55,7 +55,7 @@ error[E0599]: no method named `boxed_closure` found for struct `BoxedObj` in the
5555
--> $DIR/issue-2392.rs:48:19
5656
|
5757
LL | struct BoxedObj {
58-
| -------- method `boxed_closure` not found for this struct
58+
| --------------- method `boxed_closure` not found for this struct
5959
...
6060
LL | boxed_closure.boxed_closure();
6161
| ^^^^^^^^^^^^^ field, not a method
@@ -69,7 +69,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
6969
--> $DIR/issue-2392.rs:53:12
7070
|
7171
LL | struct Obj<F> where F: FnOnce() -> u32 {
72-
| --- method `closure` not found for this struct
72+
| ------------- method `closure` not found for this struct
7373
...
7474
LL | w.wrap.closure();
7575
| ^^^^^^^ field, not a method
@@ -83,7 +83,7 @@ error[E0599]: no method named `not_closure` found for struct `Obj` in the curren
8383
--> $DIR/issue-2392.rs:55:12
8484
|
8585
LL | struct Obj<F> where F: FnOnce() -> u32 {
86-
| --- method `not_closure` not found for this struct
86+
| ------------- method `not_closure` not found for this struct
8787
...
8888
LL | w.wrap.not_closure();
8989
| ^^^^^^^^^^^-- help: remove the arguments
@@ -94,7 +94,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
9494
--> $DIR/issue-2392.rs:58:24
9595
|
9696
LL | struct Obj<F> where F: FnOnce() -> u32 {
97-
| --- method `closure` not found for this struct
97+
| ------------- method `closure` not found for this struct
9898
...
9999
LL | check_expression().closure();
100100
| ^^^^^^^ field, not a method
@@ -108,7 +108,7 @@ error[E0599]: no method named `f1` found for struct `FuncContainer` in the curre
108108
--> $DIR/issue-2392.rs:64:31
109109
|
110110
LL | struct FuncContainer {
111-
| ------------- method `f1` not found for this struct
111+
| -------------------- method `f1` not found for this struct
112112
...
113113
LL | (*self.container).f1(1);
114114
| ^^ field, not a method
@@ -122,7 +122,7 @@ error[E0599]: no method named `f2` found for struct `FuncContainer` in the curre
122122
--> $DIR/issue-2392.rs:65:31
123123
|
124124
LL | struct FuncContainer {
125-
| ------------- method `f2` not found for this struct
125+
| -------------------- method `f2` not found for this struct
126126
...
127127
LL | (*self.container).f2(1);
128128
| ^^ field, not a method
@@ -136,7 +136,7 @@ error[E0599]: no method named `f3` found for struct `FuncContainer` in the curre
136136
--> $DIR/issue-2392.rs:66:31
137137
|
138138
LL | struct FuncContainer {
139-
| ------------- method `f3` not found for this struct
139+
| -------------------- method `f3` not found for this struct
140140
...
141141
LL | (*self.container).f3(1);
142142
| ^^ field, not a method

src/test/ui/confuse-field-and-method/issue-32128.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `example` found for struct `Example` in the curren
22
--> $DIR/issue-32128.rs:12:10
33
|
44
LL | struct Example {
5-
| ------- method `example` not found for this struct
5+
| -------------- method `example` not found for this struct
66
...
77
LL | demo.example(1);
88
| ^^^^^^^ field, not a method

src/test/ui/confuse-field-and-method/private-field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `dog_age` found for struct `Dog` in the current sc
22
--> $DIR/private-field.rs:16:23
33
|
44
LL | pub struct Dog {
5-
| --- method `dog_age` not found for this struct
5+
| -------------- method `dog_age` not found for this struct
66
...
77
LL | let dog_age = dog.dog_age();
88
| ^^^^^^^ private field, not a method

src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0599]: the function or associated item `foo` exists for struct `Foo<{_: u
88
--> $DIR/issue-69654.rs:17:10
99
|
1010
LL | struct Foo<const N: usize> {}
11-
| --- function or associated item `foo` not found for this struct
11+
| -------------------------- function or associated item `foo` not found for this struct
1212
...
1313
LL | Foo::foo();
1414
| ^^^ function or associated item cannot be called on `Foo<{_: usize}>` due to unsatisfied trait bounds

src/test/ui/const-generics/generic_const_exprs/issue-80742.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0599]: the function or associated item `new` exists for struct `Inline<dy
1616
--> $DIR/issue-80742.rs:30:36
1717
|
1818
LL | struct Inline<T>
19-
| ------ function or associated item `new` not found for this struct
19+
| ---------------- function or associated item `new` not found for this struct
2020
...
2121
LL | let dst = Inline::<dyn Debug>::new(0);
2222
| ^^^ function or associated item cannot be called on `Inline<dyn Debug>` due to unsatisfied trait bounds

src/test/ui/const-generics/invalid-const-arg-for-type-param.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0599]: no method named `f` found for struct `S` in the current scope
1616
--> $DIR/invalid-const-arg-for-type-param.rs:9:7
1717
|
1818
LL | struct S;
19-
| - method `f` not found for this struct
19+
| -------- method `f` not found for this struct
2020
...
2121
LL | S.f::<0>();
2222
| ^ method not found in `S`

src/test/ui/consts/const-needs_drop-monomorphic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `assert` found for struct `Bo
22
--> $DIR/const-needs_drop-monomorphic.rs:11:46
33
|
44
LL | struct Bool<const B: bool> {}
5-
| ---- function or associated item `assert` not found for this struct
5+
| -------------------------- function or associated item `assert` not found for this struct
66
...
77
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
88
| ^^^^^^ function or associated item cannot be called on `Bool<{ std::mem::needs_drop::<T>() }>` due to unsatisfied trait bounds

src/test/ui/copy-a-resource.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `clone` found for struct `Foo` in the current scop
22
--> $DIR/copy-a-resource.rs:18:16
33
|
44
LL | struct Foo {
5-
| --- method `clone` not found for this struct
5+
| ---------- method `clone` not found for this struct
66
...
77
LL | let _y = x.clone();
88
| ^^^^^ method not found in `Foo`

src/test/ui/derives/derive-assoc-type-not-impl.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ error[E0599]: the method `clone` exists for struct `Bar<NotClone>`, but its trai
33
|
44
LL | struct Bar<T: Foo> {
55
| ------------------
6-
| | |
7-
| | method `clone` not found for this struct
6+
| |
7+
| method `clone` not found for this struct
88
| doesn't satisfy `Bar<NotClone>: Clone`
99
...
1010
LL | struct NotClone;

src/test/ui/derives/issue-91492.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LL | pub struct NoDerives;
3737
| -------------------- doesn't satisfy `NoDerives: Clone`
3838
...
3939
LL | struct Object<T, A>(T, A);
40-
| ------ method `use_clone` not found for this struct
40+
| ------------------- method `use_clone` not found for this struct
4141
...
4242
LL | foo.use_clone();
4343
| ^^^^^^^^^ method cannot be called on `Object<NoDerives, SomeDerives>` due to unsatisfied trait bounds

src/test/ui/derives/issue-91550.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | pub struct NoDerives;
2525
| -------------------- doesn't satisfy `NoDerives: Eq`
2626
LL |
2727
LL | struct Object<T>(T);
28-
| ------ method `use_eq` not found for this struct
28+
| ---------------- method `use_eq` not found for this struct
2929
...
3030
LL | foo.use_eq();
3131
| ^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
@@ -44,7 +44,7 @@ LL | pub struct NoDerives;
4444
| -------------------- doesn't satisfy `NoDerives: Ord`
4545
LL |
4646
LL | struct Object<T>(T);
47-
| ------ method `use_ord` not found for this struct
47+
| ---------------- method `use_ord` not found for this struct
4848
...
4949
LL | foo.use_ord();
5050
| ^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
@@ -66,7 +66,7 @@ LL | pub struct NoDerives;
6666
| doesn't satisfy `NoDerives: PartialOrd`
6767
LL |
6868
LL | struct Object<T>(T);
69-
| ------ method `use_ord_and_partial_ord` not found for this struct
69+
| ---------------- method `use_ord_and_partial_ord` not found for this struct
7070
...
7171
LL | foo.use_ord_and_partial_ord();
7272
| ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds

src/test/ui/did_you_mean/issue-40006.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ error[E0599]: no method named `hello_method` found for struct `S` in the current
8888
--> $DIR/issue-40006.rs:38:7
8989
|
9090
LL | struct S;
91-
| - method `hello_method` not found for this struct
91+
| -------- method `hello_method` not found for this struct
9292
...
9393
LL | S.hello_method();
9494
| ^^^^^^^^^^^^ method not found in `S`

src/test/ui/dont-suggest-private-trait-method.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `new` found for struct `T` in
22
--> $DIR/dont-suggest-private-trait-method.rs:4:8
33
|
44
LL | struct T;
5-
| - function or associated item `new` not found for this struct
5+
| -------- function or associated item `new` not found for this struct
66
...
77
LL | T::new();
88
| ^^^ function or associated item not found in `T`

src/test/ui/error-codes/E0599.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no associated item named `NotEvenReal` found for struct `Foo` in t
22
--> $DIR/E0599.rs:4:20
33
|
44
LL | struct Foo;
5-
| --- associated item `NotEvenReal` not found for this struct
5+
| ---------- associated item `NotEvenReal` not found for this struct
66
...
77
LL | || if let Foo::NotEvenReal() = Foo {};
88
| ^^^^^^^^^^^ associated item not found in `Foo`

src/test/ui/generic-associated-types/method-unsatified-assoc-type-predicate.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ error[E0599]: the method `f` exists for struct `S`, but its trait bounds were no
33
|
44
LL | struct S;
55
| --------
6-
| | |
7-
| | method `f` not found for this struct
6+
| |
7+
| method `f` not found for this struct
88
| doesn't satisfy `<S as X>::Y<i32> = i32`
99
| doesn't satisfy `S: M`
1010
...

0 commit comments

Comments
 (0)