Skip to content

Commit e2e9a31

Browse files
committed
fix #105788, Remove unreasonable help message for auto trait
1 parent 998e1a9 commit e2e9a31

File tree

5 files changed

+33
-51
lines changed

5 files changed

+33
-51
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// or type checking or some other kind of complex analysis.
88

99
use itertools::{Either, Itertools};
10-
use rustc_ast::ptr::P;
1110
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1211
use rustc_ast::walk_list;
1312
use rustc_ast::*;
@@ -644,27 +643,6 @@ impl<'a> AstValidator<'a> {
644643
}
645644
}
646645

647-
fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
648-
if !trait_items.is_empty() {
649-
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
650-
let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
651-
struct_span_err!(
652-
self.session,
653-
spans,
654-
E0380,
655-
"auto traits cannot have associated items"
656-
)
657-
.span_suggestion(
658-
total_span,
659-
"remove these associated items",
660-
"",
661-
Applicability::MachineApplicable,
662-
)
663-
.span_label(ident_span, "auto trait cannot have associated items")
664-
.emit();
665-
}
666-
}
667-
668646
fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String {
669647
// Lifetimes always come first.
670648
let lt_sugg = data.args.iter().filter_map(|arg| match arg {
@@ -1152,7 +1130,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11521130
self.deny_generic_params(generics, item.ident.span);
11531131
self.deny_super_traits(bounds, item.ident.span);
11541132
self.deny_where_clause(&generics.where_clause, item.ident.span);
1155-
self.deny_items(items, item.ident.span);
11561133
}
11571134

11581135
// Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound

compiler/rustc_hir_typeck/src/method/suggest.rs

-10
Original file line numberDiff line numberDiff line change
@@ -689,16 +689,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
689689
let entry = spanned_predicates.entry(spans);
690690
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
691691
}
692-
Some(Node::Item(hir::Item {
693-
kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
694-
span: item_span,
695-
..
696-
})) => {
697-
tcx.sess.delay_span_bug(
698-
*item_span,
699-
"auto trait is invoked with no method error, but no error reported?",
700-
);
701-
}
702692
Some(_) => unreachable!(),
703693
None => (),
704694
}

compiler/rustc_parse/src/parser/item.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, Vari
1616
use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
1717
use rustc_ast::{MacCall, MacDelimiter};
1818
use rustc_ast_pretty::pprust;
19-
use rustc_errors::{struct_span_err, Applicability, IntoDiagnostic, PResult, StashKey};
19+
use rustc_errors::{error_code, struct_span_err, Applicability, IntoDiagnostic, PResult, StashKey};
2020
use rustc_span::edition::Edition;
2121
use rustc_span::lev_distance::lev_distance;
2222
use rustc_span::source_map::{self, Span};
@@ -870,14 +870,39 @@ impl<'a> Parser<'a> {
870870
} else {
871871
// It's a normal trait.
872872
generics.where_clause = self.parse_where_clause()?;
873-
let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
873+
let mut items =
874+
self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
875+
if items.len() > 0 && is_auto == IsAuto::Yes {
876+
self.deny_items(&items, ident.span);
877+
items = vec![];
878+
}
874879
Ok((
875880
ident,
876881
ItemKind::Trait(Box::new(Trait { is_auto, unsafety, generics, bounds, items })),
877882
))
878883
}
879884
}
880885

886+
fn deny_items(&self, items: &Vec<P<Item<AssocItemKind>>>, ident_span: Span) {
887+
let spans: Vec<_> = items.iter().map(|i| i.ident.span).collect();
888+
let total_span = items.first().unwrap().span.to(items.last().unwrap().span);
889+
self.sess
890+
.span_diagnostic
891+
.struct_span_err_with_code(
892+
spans,
893+
"auto traits cannot have associated items",
894+
error_code!(E0753),
895+
)
896+
.span_suggestion(
897+
total_span,
898+
"remove these associated items",
899+
"",
900+
Applicability::MachineApplicable,
901+
)
902+
.span_label(ident_span, "auto trait cannot have associated items")
903+
.emit();
904+
}
905+
881906
pub fn parse_impl_item(
882907
&mut self,
883908
force_collect: ForceCollect,

src/test/ui/methods/issues/issue-105732.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ auto trait Foo {
66

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

Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
error[E0380]: auto traits cannot have associated items
1+
error[E0753]: auto traits cannot have associated items
22
--> $DIR/issue-105732.rs:4:8
33
|
44
LL | auto trait Foo {
55
| --- auto trait cannot have associated items
66
LL | fn g(&self);
77
| ---^-------- help: remove these associated items
88

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

2515
error: aborting due to 2 previous errors
2616

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

0 commit comments

Comments
 (0)