Skip to content

Commit aacbd8e

Browse files
Add a helpful suggestion
1 parent a0b821c commit aacbd8e

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
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` 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
@@ -102,6 +102,8 @@ pub struct MisplacedImplTrait<'a> {
102102
pub struct MisplacedAssocTyBinding {
103103
#[primary_span]
104104
pub span: Span,
105+
#[suggestion(code = " = impl", applicability = "maybe-incorrect", style = "verbose")]
106+
pub suggestion: Option<Span>,
105107
}
106108

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

compiler/rustc_ast_lowering/src/lib.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10811081
AssocConstraintKind::Bound { bounds } => {
10821082
enum DesugarKind {
10831083
ImplTrait,
1084-
Error,
1084+
Error(Option<Span>),
10851085
Bound,
10861086
}
10871087

10881088
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
10891089
let desugar_kind = match itctx {
1090-
_ if self.is_in_dyn_type => DesugarKind::Error,
1090+
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1091+
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1092+
| ImplTraitContext::Universal
1093+
if self.is_in_dyn_type =>
1094+
{
1095+
let bound_end_span = constraint
1096+
.gen_args
1097+
.as_ref()
1098+
.map_or(constraint.ident.span, |args| args.span());
1099+
let colon_span = if bound_end_span.eq_ctxt(constraint.span) {
1100+
Some(self.tcx.sess.source_map().next_point(bound_end_span))
1101+
} else {
1102+
None
1103+
};
1104+
DesugarKind::Error(colon_span)
1105+
}
1106+
_ if self.is_in_dyn_type => DesugarKind::Error(None),
10911107

10921108
// We are in the return position:
10931109
//
@@ -1143,10 +1159,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11431159

11441160
hir::TypeBindingKind::Constraint { bounds }
11451161
}
1146-
DesugarKind::Error => {
1147-
let guar = self
1148-
.dcx()
1149-
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
1162+
DesugarKind::Error(suggestion) => {
1163+
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1164+
span: constraint.span,
1165+
suggestion,
1166+
});
11501167
let err_ty =
11511168
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
11521169
hir::TypeBindingKind::Equality { term: err_ty.into() }

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` 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/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` 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)