Skip to content

Commit 7ee1b47

Browse files
committed
review comments
1 parent 29be741 commit 7ee1b47

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

src/librustc_ast_passes/ast_validation.rs

+52-51
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use rustc_ast::ast::*;
1010
use rustc_ast::attr;
1111
use rustc_ast::expand::is_proc_macro_attr;
12+
use rustc_ast::ptr::P;
1213
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
1314
use rustc_ast::walk_list;
1415
use rustc_ast_pretty::pprust;
@@ -594,6 +595,54 @@ impl<'a> AstValidator<'a> {
594595
.span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
595596
.emit();
596597
}
598+
599+
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
600+
if !generics.params.is_empty() {
601+
struct_span_err!(
602+
self.session,
603+
generics.span,
604+
E0567,
605+
"auto traits cannot have generic parameters"
606+
)
607+
.span_label(ident_span, "auto trait cannot have generic parameters")
608+
.span_suggestion(
609+
generics.span,
610+
"remove the parameters",
611+
String::new(),
612+
Applicability::MachineApplicable,
613+
)
614+
.emit();
615+
}
616+
}
617+
618+
fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
619+
if let [first @ last] | [first, .., last] = &bounds[..] {
620+
let span = first.span().to(last.span());
621+
struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits")
622+
.span_label(ident_span, "auto trait cannot have super traits")
623+
.span_suggestion(
624+
span,
625+
"remove the super traits",
626+
String::new(),
627+
Applicability::MachineApplicable,
628+
)
629+
.emit();
630+
}
631+
}
632+
633+
fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
634+
if !trait_items.is_empty() {
635+
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
636+
struct_span_err!(
637+
self.session,
638+
spans,
639+
E0380,
640+
"auto traits cannot have methods or associated items"
641+
)
642+
.span_label(ident_span, "auto trait cannot have items")
643+
.emit();
644+
}
645+
}
597646
}
598647

599648
fn validate_generic_param_order<'a>(
@@ -881,57 +930,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881930
ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
882931
if is_auto == IsAuto::Yes {
883932
// Auto traits cannot have generics, super traits nor contain items.
884-
if !generics.params.is_empty() {
885-
let mut err = struct_span_err!(
886-
self.session,
887-
generics.span,
888-
E0567,
889-
"auto traits cannot have generic parameters"
890-
);
891-
err.span_label(
892-
item.ident.span,
893-
"auto trait cannot have generic parameters",
894-
);
895-
err.span_suggestion(
896-
generics.span,
897-
"remove the parameters",
898-
String::new(),
899-
Applicability::MachineApplicable,
900-
);
901-
err.emit();
902-
}
903-
if !bounds.is_empty() {
904-
let span = match &bounds[..] {
905-
[] => unreachable!(),
906-
[single] => single.span(),
907-
[first, .., last] => first.span().to(last.span()),
908-
};
909-
let mut err = struct_span_err!(
910-
self.session,
911-
span,
912-
E0568,
913-
"auto traits cannot have super traits"
914-
);
915-
err.span_label(item.ident.span, "auto trait cannot have super traits");
916-
err.span_suggestion(
917-
span,
918-
"remove the super traits",
919-
String::new(),
920-
Applicability::MachineApplicable,
921-
);
922-
err.emit();
923-
}
924-
if !trait_items.is_empty() {
925-
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
926-
struct_span_err!(
927-
self.session,
928-
spans,
929-
E0380,
930-
"auto traits cannot have methods or associated items"
931-
)
932-
.span_label(item.ident.span, "auto trait cannot have items")
933-
.emit();
934-
}
933+
self.deny_generic_params(generics, item.ident.span);
934+
self.deny_super_traits(bounds, item.ident.span);
935+
self.deny_items(trait_items, item.ident.span);
935936
}
936937
self.no_questions_in_bounds(bounds, "supertraits", true);
937938

src/librustc_ast_passes/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(bindings_after_at)]
12
//! The `rustc_ast_passes` crate contains passes which validate the AST in `syntax`
23
//! parsed by `rustc_parse` and then lowered, after the passes in this crate,
34
//! by `rustc_ast_lowering`.

0 commit comments

Comments
 (0)