Skip to content

Commit

Permalink
fix #105788, Remove unreasonable help message for auto trait
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Dec 17, 2022
1 parent 998e1a9 commit e2e9a31
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 51 deletions.
23 changes: 0 additions & 23 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// or type checking or some other kind of complex analysis.

use itertools::{Either, Itertools};
use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
use rustc_ast::walk_list;
use rustc_ast::*;
Expand Down Expand Up @@ -644,27 +643,6 @@ impl<'a> AstValidator<'a> {
}
}

fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
if !trait_items.is_empty() {
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
struct_span_err!(
self.session,
spans,
E0380,
"auto traits cannot have associated items"
)
.span_suggestion(
total_span,
"remove these associated items",
"",
Applicability::MachineApplicable,
)
.span_label(ident_span, "auto trait cannot have associated items")
.emit();
}
}

fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String {
// Lifetimes always come first.
let lt_sugg = data.args.iter().filter_map(|arg| match arg {
Expand Down Expand Up @@ -1152,7 +1130,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.deny_generic_params(generics, item.ident.span);
self.deny_super_traits(bounds, item.ident.span);
self.deny_where_clause(&generics.where_clause, item.ident.span);
self.deny_items(items, item.ident.span);
}

// Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
Expand Down
10 changes: 0 additions & 10 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,16 +689,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
}
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
span: item_span,
..
})) => {
tcx.sess.delay_span_bug(
*item_span,
"auto trait is invoked with no method error, but no error reported?",
);
}
Some(_) => unreachable!(),
None => (),
}
Expand Down
29 changes: 27 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, Vari
use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
use rustc_ast::{MacCall, MacDelimiter};
use rustc_ast_pretty::pprust;
use rustc_errors::{struct_span_err, Applicability, IntoDiagnostic, PResult, StashKey};
use rustc_errors::{error_code, struct_span_err, Applicability, IntoDiagnostic, PResult, StashKey};
use rustc_span::edition::Edition;
use rustc_span::lev_distance::lev_distance;
use rustc_span::source_map::{self, Span};
Expand Down Expand Up @@ -870,14 +870,39 @@ impl<'a> Parser<'a> {
} else {
// It's a normal trait.
generics.where_clause = self.parse_where_clause()?;
let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
let mut items =
self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
if items.len() > 0 && is_auto == IsAuto::Yes {
self.deny_items(&items, ident.span);
items = vec![];
}
Ok((
ident,
ItemKind::Trait(Box::new(Trait { is_auto, unsafety, generics, bounds, items })),
))
}
}

fn deny_items(&self, items: &Vec<P<Item<AssocItemKind>>>, ident_span: Span) {
let spans: Vec<_> = items.iter().map(|i| i.ident.span).collect();
let total_span = items.first().unwrap().span.to(items.last().unwrap().span);
self.sess
.span_diagnostic
.struct_span_err_with_code(
spans,
"auto traits cannot have associated items",
error_code!(E0753),
)
.span_suggestion(
total_span,
"remove these associated items",
"",
Applicability::MachineApplicable,
)
.span_label(ident_span, "auto trait cannot have associated items")
.emit();
}

pub fn parse_impl_item(
&mut self,
force_collect: ForceCollect,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/methods/issues/issue-105732.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ auto trait Foo {

trait Bar {
fn f(&self) {
self.g(); //~ ERROR the method `g` exists for reference `&Self`, but its trait bounds were not satisfied
self.g(); //~ ERROR no method named `g` found for reference `&Self` in the current scope
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/test/ui/methods/issues/issue-105732.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
error[E0380]: auto traits cannot have associated items
error[E0753]: auto traits cannot have associated items
--> $DIR/issue-105732.rs:4:8
|
LL | auto trait Foo {
| --- auto trait cannot have associated items
LL | fn g(&self);
| ---^-------- help: remove these associated items

error[E0599]: the method `g` exists for reference `&Self`, but its trait bounds were not satisfied
error[E0599]: no method named `g` found for reference `&Self` in the current scope
--> $DIR/issue-105732.rs:9:14
|
LL | self.g();
| ^
|
= note: the following trait bounds were not satisfied:
`Self: Foo`
which is required by `&Self: Foo`
`&Self: Foo`
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `g`, perhaps you need to add a supertrait for it:
|
LL | trait Bar: Foo {
| +++++
| ^ help: there is a method with a similar name: `f`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0380, E0599.
For more information about an error, try `rustc --explain E0380`.
Some errors have detailed explanations: E0599, E0753.
For more information about an error, try `rustc --explain E0599`.

0 comments on commit e2e9a31

Please sign in to comment.