Skip to content

Commit 661ceaf

Browse files
Add a helpful suggestion
1 parent f0632b0 commit 661ceaf

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
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
@@ -1087,13 +1087,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10871087
AssocConstraintKind::Bound { bounds } => {
10881088
enum DesugarKind {
10891089
ImplTrait,
1090-
Error,
1090+
Error(Option<Span>),
10911091
Bound,
10921092
}
10931093

10941094
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
10951095
let desugar_kind = match itctx {
1096-
_ if self.is_in_dyn_type => DesugarKind::Error,
1096+
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1097+
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1098+
| ImplTraitContext::Universal
1099+
if self.is_in_dyn_type =>
1100+
{
1101+
let bound_end_span = constraint
1102+
.gen_args
1103+
.as_ref()
1104+
.map_or(constraint.ident.span, |args| args.span());
1105+
let colon_span = if bound_end_span.eq_ctxt(constraint.span) {
1106+
Some(self.tcx.sess.source_map().next_point(bound_end_span))
1107+
} else {
1108+
None
1109+
};
1110+
DesugarKind::Error(colon_span)
1111+
}
1112+
_ if self.is_in_dyn_type => DesugarKind::Error(None),
10971113

10981114
// We are in the return position:
10991115
//
@@ -1149,10 +1165,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11491165

11501166
hir::TypeBindingKind::Constraint { bounds }
11511167
}
1152-
DesugarKind::Error => {
1153-
let guar = self
1154-
.dcx()
1155-
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
1168+
DesugarKind::Error(suggestion) => {
1169+
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1170+
span: constraint.span,
1171+
suggestion,
1172+
});
11561173
let err_ty =
11571174
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
11581175
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/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` 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)