Add a helpful suggestion

This commit is contained in:
Michael Goulet 2024-02-06 19:48:16 +00:00
parent 973bbfbd23
commit fde695a2d1
7 changed files with 43 additions and 57 deletions

View File

@ -10,6 +10,7 @@ ast_lowering_argument = argument
ast_lowering_assoc_ty_binding_in_dyn = ast_lowering_assoc_ty_binding_in_dyn =
associated type bounds are not allowed in `dyn` types associated type bounds are not allowed in `dyn` types
.suggestion = use `impl Trait` to introduce a type instead
ast_lowering_assoc_ty_parentheses = ast_lowering_assoc_ty_parentheses =
parenthesized generic arguments cannot be used in associated type constraints parenthesized generic arguments cannot be used in associated type constraints

View File

@ -98,6 +98,8 @@ pub struct MisplacedImplTrait<'a> {
pub struct MisplacedAssocTyBinding { pub struct MisplacedAssocTyBinding {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
#[suggestion(code = " = impl", applicability = "maybe-incorrect", style = "verbose")]
pub suggestion: Option<Span>,
} }
#[derive(Diagnostic, Clone, Copy)] #[derive(Diagnostic, Clone, Copy)]

View File

@ -1084,42 +1084,39 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::TypeBindingKind::Equality { term } hir::TypeBindingKind::Equality { term }
} }
AssocConstraintKind::Bound { bounds } => { AssocConstraintKind::Bound { bounds } => {
enum DesugarKind { // Disallow ATB in dyn types
Error, if self.is_in_dyn_type {
Bound, let suggestion = match itctx {
ImplTraitContext::ReturnPositionOpaqueTy { .. }
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
| ImplTraitContext::Universal => {
let bound_end_span = constraint
.gen_args
.as_ref()
.map_or(constraint.ident.span, |args| args.span());
if bound_end_span.eq_ctxt(constraint.span) {
Some(self.tcx.sess.source_map().next_point(bound_end_span))
} else {
None
} }
}
// Piggy-back on the `impl Trait` context to figure out the correct behavior. _ => None,
let desugar_kind = match itctx {
_ if self.is_in_dyn_type => DesugarKind::Error,
// We are in the parameter position, but not within a dyn type:
//
// fn foo(x: impl Iterator<Item: Debug>)
//
// so we leave it as is and this gets expanded in astconv to a bound like
// `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
// `impl Iterator`.
_ => DesugarKind::Bound,
}; };
match desugar_kind { let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
DesugarKind::Bound => { span: constraint.span,
suggestion,
});
let err_ty =
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
hir::TypeBindingKind::Equality { term: err_ty.into() }
} else {
// Desugar `AssocTy: Bounds` into a type binding where the // Desugar `AssocTy: Bounds` into a type binding where the
// later desugars into a trait predicate. // later desugars into a trait predicate.
let bounds = self.lower_param_bounds(bounds, itctx); let bounds = self.lower_param_bounds(bounds, itctx);
hir::TypeBindingKind::Constraint { bounds } hir::TypeBindingKind::Constraint { bounds }
} }
DesugarKind::Error => {
let guar = self
.dcx()
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
let err_ty =
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
hir::TypeBindingKind::Equality { term: err_ty.into() }
}
}
} }
}; };

View File

@ -3,6 +3,11 @@ error: associated type bounds are not allowed in `dyn` types
| |
LL | type Out = Box<dyn Bar<Assoc: Copy>>; LL | type Out = Box<dyn Bar<Assoc: Copy>>;
| ^^^^^^^^^^^ | ^^^^^^^^^^^
|
help: use `impl Trait` to introduce a type instead
|
LL | type Out = Box<dyn Bar<Assoc = impl Copy>>;
| ~~~~~~
error: aborting due to 1 previous error error: aborting due to 1 previous error

View File

@ -1,12 +0,0 @@
warning: method `tr2` is never used
--> $DIR/dyn-impl-trait-type.rs:8:20
|
LL | trait Tr2<'a> { fn tr2(self) -> &'a Self; }
| --- ^^^
| |
| method in this trait
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted

View File

@ -1,12 +0,0 @@
warning: method `tr2` is never used
--> $DIR/dyn-rpit-and-let.rs:10:20
|
LL | trait Tr2<'a> { fn tr2(self) -> &'a Self; }
| --- ^^^
| |
| method in this trait
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted

View File

@ -15,6 +15,11 @@ error: associated type bounds are not allowed in `dyn` types
| |
LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() } LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use `impl Trait` to introduce a type instead
|
LL | fn f(x: &mut dyn Iterator<Item = impl Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
| ~~~~~~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors