Skip to content

Commit fde695a

Browse files
Add a helpful suggestion
1 parent 973bbfb commit fde695a

File tree

7 files changed

+43
-57
lines changed

7 files changed

+43
-57
lines changed

compiler/rustc_ast_lowering/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ast_lowering_argument = argument
1010
1111
ast_lowering_assoc_ty_binding_in_dyn =
1212
associated type bounds are not allowed in `dyn` types
13+
.suggestion = use `impl Trait` to introduce a type instead
1314
1415
ast_lowering_assoc_ty_parentheses =
1516
parenthesized generic arguments cannot be used in associated type constraints

compiler/rustc_ast_lowering/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub struct MisplacedImplTrait<'a> {
9898
pub struct MisplacedAssocTyBinding {
9999
#[primary_span]
100100
pub span: Span,
101+
#[suggestion(code = " = impl", applicability = "maybe-incorrect", style = "verbose")]
102+
pub suggestion: Option<Span>,
101103
}
102104

103105
#[derive(Diagnostic, Clone, Copy)]

compiler/rustc_ast_lowering/src/lib.rs

+30-33
Original file line numberDiff line numberDiff line change
@@ -1084,41 +1084,38 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10841084
hir::TypeBindingKind::Equality { term }
10851085
}
10861086
AssocConstraintKind::Bound { bounds } => {
1087-
enum DesugarKind {
1088-
Error,
1089-
Bound,
1090-
}
1091-
1092-
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
1093-
let desugar_kind = match itctx {
1094-
_ if self.is_in_dyn_type => DesugarKind::Error,
1095-
1096-
// We are in the parameter position, but not within a dyn type:
1097-
//
1098-
// fn foo(x: impl Iterator<Item: Debug>)
1099-
//
1100-
// so we leave it as is and this gets expanded in astconv to a bound like
1101-
// `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
1102-
// `impl Iterator`.
1103-
_ => DesugarKind::Bound,
1104-
};
1087+
// Disallow ATB in dyn types
1088+
if self.is_in_dyn_type {
1089+
let suggestion = match itctx {
1090+
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1091+
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1092+
| ImplTraitContext::Universal => {
1093+
let bound_end_span = constraint
1094+
.gen_args
1095+
.as_ref()
1096+
.map_or(constraint.ident.span, |args| args.span());
1097+
if bound_end_span.eq_ctxt(constraint.span) {
1098+
Some(self.tcx.sess.source_map().next_point(bound_end_span))
1099+
} else {
1100+
None
1101+
}
1102+
}
1103+
_ => None,
1104+
};
11051105

1106-
match desugar_kind {
1107-
DesugarKind::Bound => {
1108-
// Desugar `AssocTy: Bounds` into a type binding where the
1109-
// later desugars into a trait predicate.
1110-
let bounds = self.lower_param_bounds(bounds, itctx);
1106+
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1107+
span: constraint.span,
1108+
suggestion,
1109+
});
1110+
let err_ty =
1111+
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1112+
hir::TypeBindingKind::Equality { term: err_ty.into() }
1113+
} else {
1114+
// Desugar `AssocTy: Bounds` into a type binding where the
1115+
// later desugars into a trait predicate.
1116+
let bounds = self.lower_param_bounds(bounds, itctx);
11111117

1112-
hir::TypeBindingKind::Constraint { bounds }
1113-
}
1114-
DesugarKind::Error => {
1115-
let guar = self
1116-
.dcx()
1117-
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
1118-
let err_ty =
1119-
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1120-
hir::TypeBindingKind::Equality { term: err_ty.into() }
1121-
}
1118+
hir::TypeBindingKind::Constraint { bounds }
11221119
}
11231120
}
11241121
};

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error: associated type bounds are not allowed in `dyn` types
33
|
44
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
55
| ^^^^^^^^^^^
6+
|
7+
help: use `impl Trait` to introduce a type instead
8+
|
9+
LL | type Out = Box<dyn Bar<Assoc = impl Copy>>;
10+
| ~~~~~~
611

712
error: aborting due to 1 previous error
813

tests/ui/associated-type-bounds/dyn-impl-trait-type.stderr

-12
This file was deleted.

tests/ui/associated-type-bounds/dyn-rpit-and-let.stderr

-12
This file was deleted.

tests/ui/associated-type-bounds/elision.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ error: associated type bounds are not allowed in `dyn` types
1515
|
1616
LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: use `impl Trait` to introduce a type instead
20+
|
21+
LL | fn f(x: &mut dyn Iterator<Item = impl Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
22+
| ~~~~~~
1823

1924
error: aborting due to 2 previous errors
2025

0 commit comments

Comments
 (0)