1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Validate AST before lowering it to HIR
13 // This pass is supposed to catch things that fit into AST data structures,
14 // but not permitted by the language. It runs after expansion when AST is frozen,
15 // so it can check for erroneous constructions produced by syntax extensions.
16 // This pass is supposed to perform only simple checks not requiring name resolution
17 // or type checking or some other kind of complex analysis.
20 use rustc::session::Session;
23 use syntax::parse::token::{self, keywords};
24 use syntax::visit::{self, Visitor};
28 struct AstValidator<'a> {
32 impl<'a> AstValidator<'a> {
33 fn err_handler(&self) -> &errors::Handler {
34 &self.session.parse_sess.span_diagnostic
37 fn check_label(&self, label: Ident, span: Span, id: NodeId) {
38 if label.name == keywords::StaticLifetime.name() {
39 self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name));
41 if label.name.as_str() == "'_" {
42 self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE,
45 format!("invalid label name `{}`", label.name));
49 fn invalid_visibility(&self, vis: &Visibility, span: Span, note: Option<&str>) {
50 if vis != &Visibility::Inherited {
51 let mut err = struct_span_err!(self.session,
54 "unnecessary visibility qualifier");
55 if let Some(note) = note {
56 err.span_note(span, note);
62 fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
63 for arg in &decl.inputs {
65 PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
67 PatKind::Ident(..) => report_err(arg.pat.span, true),
68 _ => report_err(arg.pat.span, false),
74 impl<'a> Visitor for AstValidator<'a> {
75 fn visit_lifetime(&mut self, lt: &Lifetime) {
76 if lt.name.as_str() == "'_" {
77 self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE,
80 format!("invalid lifetime name `{}`", lt.name));
83 visit::walk_lifetime(self, lt)
86 fn visit_expr(&mut self, expr: &Expr) {
88 ExprKind::While(_, _, Some(ident)) |
89 ExprKind::Loop(_, Some(ident)) |
90 ExprKind::WhileLet(_, _, _, Some(ident)) |
91 ExprKind::ForLoop(_, _, _, Some(ident)) |
92 ExprKind::Break(Some(ident)) |
93 ExprKind::Continue(Some(ident)) => {
94 self.check_label(ident.node, ident.span, expr.id);
99 visit::walk_expr(self, expr)
102 fn visit_ty(&mut self, ty: &Ty) {
104 TyKind::BareFn(ref bfty) => {
105 self.check_decl_no_pat(&bfty.decl, |span, _| {
106 let mut err = struct_span_err!(self.session,
109 "patterns aren't allowed in function pointer \
112 "this is a recent error, see issue #35203 for more details");
119 visit::walk_ty(self, ty)
122 fn visit_path(&mut self, path: &Path, id: NodeId) {
123 if path.global && path.segments.len() > 0 {
124 let ident = path.segments[0].identifier;
125 if token::Ident(ident).is_path_segment_keyword() {
126 self.session.add_lint(lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH,
129 format!("global paths cannot start with `{}`", ident));
133 visit::walk_path(self, path)
136 fn visit_item(&mut self, item: &Item) {
138 ItemKind::Use(ref view_path) => {
139 let path = view_path.node.path();
140 if !path.segments.iter().all(|segment| segment.parameters.is_empty()) {
142 .span_err(path.span, "type or lifetime parameters in import path");
145 ItemKind::Impl(_, _, _, Some(..), _, ref impl_items) => {
146 self.invalid_visibility(&item.vis, item.span, None);
147 for impl_item in impl_items {
148 self.invalid_visibility(&impl_item.vis, impl_item.span, None);
151 ItemKind::Impl(_, _, _, None, _, _) => {
152 self.invalid_visibility(&item.vis,
154 Some("place qualifiers on individual impl items instead"));
156 ItemKind::DefaultImpl(..) => {
157 self.invalid_visibility(&item.vis, item.span, None);
159 ItemKind::ForeignMod(..) => {
160 self.invalid_visibility(&item.vis,
162 Some("place qualifiers on individual foreign items \
165 ItemKind::Enum(ref def, _) => {
166 for variant in &def.variants {
167 for field in variant.node.data.fields() {
168 self.invalid_visibility(&field.vis, field.span, None);
172 ItemKind::Mod(_) => {
173 // Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
174 attr::first_attr_value_str_by_name(&item.attrs, "path");
179 visit::walk_item(self, item)
182 fn visit_foreign_item(&mut self, fi: &ForeignItem) {
184 ForeignItemKind::Fn(ref decl, _) => {
185 self.check_decl_no_pat(decl, |span, is_recent| {
186 let mut err = struct_span_err!(self.session,
189 "patterns aren't allowed in foreign function \
191 err.span_label(span, &format!("pattern not allowed in foreign function"));
194 "this is a recent error, see issue #35203 for more details");
199 ForeignItemKind::Static(..) => {}
202 visit::walk_foreign_item(self, fi)
205 fn visit_vis(&mut self, vis: &Visibility) {
207 Visibility::Restricted { ref path, .. } => {
208 if !path.segments.iter().all(|segment| segment.parameters.is_empty()) {
210 .span_err(path.span, "type or lifetime parameters in visibility path");
216 visit::walk_vis(self, vis)
220 pub fn check_crate(session: &Session, krate: &Crate) {
221 visit::walk_crate(&mut AstValidator { session: session }, krate)