Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 11, 2020
1 parent 29be741 commit 7ee1b47
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
103 changes: 52 additions & 51 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use rustc_ast::ast::*;
use rustc_ast::attr;
use rustc_ast::expand::is_proc_macro_attr;
use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
use rustc_ast::walk_list;
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -594,6 +595,54 @@ impl<'a> AstValidator<'a> {
.span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
.emit();
}

fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
if !generics.params.is_empty() {
struct_span_err!(
self.session,
generics.span,
E0567,
"auto traits cannot have generic parameters"
)
.span_label(ident_span, "auto trait cannot have generic parameters")
.span_suggestion(
generics.span,
"remove the parameters",
String::new(),
Applicability::MachineApplicable,
)
.emit();
}
}

fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
if let [first @ last] | [first, .., last] = &bounds[..] {
let span = first.span().to(last.span());
struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits")
.span_label(ident_span, "auto trait cannot have super traits")
.span_suggestion(
span,
"remove the super traits",
String::new(),
Applicability::MachineApplicable,
)
.emit();
}
}

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();
struct_span_err!(
self.session,
spans,
E0380,
"auto traits cannot have methods or associated items"
)
.span_label(ident_span, "auto trait cannot have items")
.emit();
}
}
}

fn validate_generic_param_order<'a>(
Expand Down Expand Up @@ -881,57 +930,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
if is_auto == IsAuto::Yes {
// Auto traits cannot have generics, super traits nor contain items.
if !generics.params.is_empty() {
let mut err = struct_span_err!(
self.session,
generics.span,
E0567,
"auto traits cannot have generic parameters"
);
err.span_label(
item.ident.span,
"auto trait cannot have generic parameters",
);
err.span_suggestion(
generics.span,
"remove the parameters",
String::new(),
Applicability::MachineApplicable,
);
err.emit();
}
if !bounds.is_empty() {
let span = match &bounds[..] {
[] => unreachable!(),
[single] => single.span(),
[first, .., last] => first.span().to(last.span()),
};
let mut err = struct_span_err!(
self.session,
span,
E0568,
"auto traits cannot have super traits"
);
err.span_label(item.ident.span, "auto trait cannot have super traits");
err.span_suggestion(
span,
"remove the super traits",
String::new(),
Applicability::MachineApplicable,
);
err.emit();
}
if !trait_items.is_empty() {
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
struct_span_err!(
self.session,
spans,
E0380,
"auto traits cannot have methods or associated items"
)
.span_label(item.ident.span, "auto trait cannot have items")
.emit();
}
self.deny_generic_params(generics, item.ident.span);
self.deny_super_traits(bounds, item.ident.span);
self.deny_items(trait_items, item.ident.span);
}
self.no_questions_in_bounds(bounds, "supertraits", true);

Expand Down
1 change: 1 addition & 0 deletions src/librustc_ast_passes/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(bindings_after_at)]
//! The `rustc_ast_passes` crate contains passes which validate the AST in `syntax`
//! parsed by `rustc_parse` and then lowered, after the passes in this crate,
//! by `rustc_ast_lowering`.
Expand Down

0 comments on commit 7ee1b47

Please sign in to comment.