Skip to content

Commit 7a08447

Browse files
committed
Suggest assoc ty bound on bare dyn trait in eq constraint
1 parent add4156 commit 7a08447

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

compiler/rustc_hir_analysis/src/astconv/lint.rs

+34
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
6868
}
6969
// Check if the impl trait that we are considering is an impl of a local trait.
7070
self.maybe_suggest_blanket_trait_impl(self_ty, &mut diag);
71+
self.maybe_suggest_assoc_ty_bound(self_ty, &mut diag);
7172
diag.stash(self_ty.span, StashKey::TraitMissingMethod);
7273
} else {
7374
let msg = "trait objects without an explicit `dyn` are deprecated";
@@ -153,6 +154,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
153154
fn maybe_suggest_impl_trait(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) -> bool {
154155
let tcx = self.tcx();
155156
let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id;
157+
// FIXME: If `type_alias_impl_trait` is enabled, also look for `Trait0<Ty = Trait1>`
158+
// and suggest `Trait0<Ty = impl Trait1>`.
156159
let (sig, generics, owner) = match tcx.hir_node_by_def_id(parent_id) {
157160
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, generics, _), .. }) => {
158161
(sig, generics, None)
@@ -260,4 +263,35 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
260263
}
261264
false
262265
}
266+
267+
// FIXME(fmease): Make this translatable.
268+
fn maybe_suggest_assoc_ty_bound(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) {
269+
let mut parents = self.tcx().hir().parent_iter(self_ty.hir_id);
270+
271+
if let Some((_, hir::Node::TypeBinding(binding))) = parents.next()
272+
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(obj_ty) } = binding.kind
273+
{
274+
if let Some((_, hir::Node::TraitRef(..))) = parents.next()
275+
&& let Some((_, hir::Node::Ty(ty))) = parents.next()
276+
&& let hir::TyKind::TraitObject(..) = ty.kind
277+
{
278+
// Assoc ty bounds aren't permitted inside trait object types.
279+
return;
280+
}
281+
282+
let lo = if binding.gen_args.span_ext.is_dummy() {
283+
binding.ident.span
284+
} else {
285+
binding.gen_args.span_ext
286+
};
287+
let span = lo.between(obj_ty.span);
288+
289+
diag.span_suggestion_verbose(
290+
span,
291+
"you might have meant to write a bound here",
292+
": ",
293+
Applicability::MaybeIncorrect,
294+
);
295+
}
296+
}
263297
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Regression test for issue #105056.
2+
//@ edition: 2021
3+
4+
fn f(_: impl Trait<T = Copy>) {}
5+
//~^ ERROR trait objects must include the `dyn` keyword
6+
//~| HELP add `dyn` keyword before this trait
7+
//~| HELP you might have meant to write a bound here
8+
//~| ERROR the trait `Copy` cannot be made into an object
9+
10+
fn g(_: impl Trait<T = std::fmt::Debug + Eq>) {}
11+
//~^ ERROR trait objects must include the `dyn` keyword
12+
//~| HELP add `dyn` keyword before this trait
13+
//~| HELP you might have meant to write a bound here
14+
//~| ERROR only auto traits can be used as additional traits in a trait object
15+
//~| HELP consider creating a new trait
16+
//~| ERROR the trait `Eq` cannot be made into an object
17+
18+
fn h(_: impl Trait<T<> = 'static + for<'a> Fn(&'a ())>) {}
19+
//~^ ERROR trait objects must include the `dyn` keyword
20+
//~| HELP add `dyn` keyword before this trait
21+
//~| HELP you might have meant to write a bound here
22+
23+
// Don't suggest assoc ty bound in trait object types, that's not valid:
24+
type Obj = dyn Trait<T = Clone>;
25+
//~^ ERROR trait objects must include the `dyn` keyword
26+
//~| HELP add `dyn` keyword before this trait
27+
28+
trait Trait { type T; }
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
error[E0225]: only auto traits can be used as additional traits in a trait object
2+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:10:42
3+
|
4+
LL | fn g(_: impl Trait<T = std::fmt::Debug + Eq>) {}
5+
| --------------- ^^ additional non-auto trait
6+
| |
7+
| first non-auto trait
8+
|
9+
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Debug + Eq {}`
10+
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
11+
12+
error[E0038]: the trait `Eq` cannot be made into an object
13+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:10:24
14+
|
15+
LL | fn g(_: impl Trait<T = std::fmt::Debug + Eq>) {}
16+
| ^^^^^^^^^^^^^^^^^^^^ `Eq` cannot be made into an object
17+
|
18+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
19+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
20+
|
21+
= note: the trait cannot be made into an object because it uses `Self` as a type parameter
22+
23+
error[E0038]: the trait `Copy` cannot be made into an object
24+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:4:20
25+
|
26+
LL | fn f(_: impl Trait<T = Copy>) {}
27+
| ^^^^^^^^ `Copy` cannot be made into an object
28+
|
29+
= note: the trait cannot be made into an object because it requires `Self: Sized`
30+
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
31+
32+
error[E0782]: trait objects must include the `dyn` keyword
33+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:4:24
34+
|
35+
LL | fn f(_: impl Trait<T = Copy>) {}
36+
| ^^^^
37+
|
38+
help: add `dyn` keyword before this trait
39+
|
40+
LL | fn f(_: impl Trait<T = dyn Copy>) {}
41+
| +++
42+
help: you might have meant to write a bound here
43+
|
44+
LL | fn f(_: impl Trait<T: Copy>) {}
45+
| ~
46+
47+
error[E0782]: trait objects must include the `dyn` keyword
48+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:10:24
49+
|
50+
LL | fn g(_: impl Trait<T = std::fmt::Debug + Eq>) {}
51+
| ^^^^^^^^^^^^^^^^^^^^
52+
|
53+
help: add `dyn` keyword before this trait
54+
|
55+
LL | fn g(_: impl Trait<T = dyn std::fmt::Debug + Eq>) {}
56+
| +++
57+
help: you might have meant to write a bound here
58+
|
59+
LL | fn g(_: impl Trait<T: std::fmt::Debug + Eq>) {}
60+
| ~
61+
62+
error[E0782]: trait objects must include the `dyn` keyword
63+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:18:26
64+
|
65+
LL | fn h(_: impl Trait<T<> = 'static + for<'a> Fn(&'a ())>) {}
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
help: add `dyn` keyword before this trait
69+
|
70+
LL | fn h(_: impl Trait<T<> = dyn 'static + for<'a> Fn(&'a ())>) {}
71+
| +++
72+
help: you might have meant to write a bound here
73+
|
74+
LL | fn h(_: impl Trait<T<>: 'static + for<'a> Fn(&'a ())>) {}
75+
| ~
76+
77+
error[E0782]: trait objects must include the `dyn` keyword
78+
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:24:26
79+
|
80+
LL | type Obj = dyn Trait<T = Clone>;
81+
| ^^^^^
82+
|
83+
help: add `dyn` keyword before this trait
84+
|
85+
LL | type Obj = dyn Trait<T = dyn Clone>;
86+
| +++
87+
88+
error: aborting due to 7 previous errors
89+
90+
Some errors have detailed explanations: E0038, E0225, E0782.
91+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)