Point out why a trait is expected on Struct + 'lt

This commit is contained in:
Esteban Küber 2020-09-22 17:35:03 -07:00
parent 5ddef544fa
commit 711760c8ec
3 changed files with 23 additions and 1 deletions

View file

@ -384,6 +384,8 @@ struct DiagnosticMetadata<'ast> {
/// Used to detect possible `if let` written without `let` and to provide structured suggestion.
in_if_condition: Option<&'ast Expr>,
current_trait_object: Option<&'ast [ast::GenericBound]>,
}
struct LateResolutionVisitor<'a, 'b, 'ast> {
@ -453,6 +455,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
self.diagnostic_metadata.current_let_binding = original;
}
fn visit_ty(&mut self, ty: &'ast Ty) {
let prev = self.diagnostic_metadata.current_trait_object;
match ty.kind {
TyKind::Path(ref qself, ref path) => {
self.smart_resolve_path(ty.id, qself.as_ref(), path, PathSource::Type);
@ -464,9 +467,13 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
.map_or(Res::Err, |d| d.res());
self.r.record_partial_res(ty.id, PartialRes::new(res));
}
TyKind::TraitObject(ref bounds, ..) => {
self.diagnostic_metadata.current_trait_object = Some(&bounds[..]);
}
_ => (),
}
visit::walk_ty(self, ty);
self.diagnostic_metadata.current_trait_object = prev;
}
fn visit_poly_trait_ref(&mut self, tref: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
self.smart_resolve_path(

View file

@ -1,6 +1,6 @@
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
use crate::late::lifetimes::{ElisionFailureInfo, LifetimeContext};
use crate::late::{LateResolutionVisitor, RibKind};
use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind};
use crate::path_names_to_string;
use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
use crate::{PathResult, PathSource, Segment};
@ -445,6 +445,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
// Fallback label.
err.span_label(base_span, fallback_label);
if let PathSource::Trait(AliasPossibility::Maybe) = source {
if let Some([start, .., end]) = self.diagnostic_metadata.current_trait_object {
err.span_help(
start.span().to(end.span()),
"`+` can be used to constrain a \"trait object\" type with lifetimes or \
auto-traits, structs and enums can't be bound in that way",
);
}
}
match self.diagnostic_metadata.current_let_binding {
Some((pat_sp, Some(ty_sp), None)) if ty_sp.contains(base_span) && could_be_expr => {
err.span_suggestion_short(

View file

@ -3,6 +3,12 @@ error[E0404]: expected trait, found struct `Foo`
|
LL | fn foo(_x: Box<Foo + Send>) { }
| ^^^ not a trait
|
help: `+` can be used to constrain a "trait object" type with lifetimes or auto-traits, structs and enums can't be bound in that way
--> $DIR/trait-bounds-not-on-struct.rs:5:16
|
LL | fn foo(_x: Box<Foo + Send>) { }
| ^^^^^^^^^^
error[E0404]: expected trait, found struct `Vec`
--> $DIR/trait-bounds-not-on-struct.rs:7:21