]>
Commit | Line | Data |
---|---|---|
3157f602 XL |
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. | |
4 | // | |
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. | |
10 | ||
11 | // Validate AST before lowering it to HIR | |
12 | // | |
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. | |
18 | ||
19 | use rustc::lint; | |
20 | use rustc::session::Session; | |
21 | use syntax::ast::*; | |
5bcae85e | 22 | use syntax::attr; |
9e0c209e | 23 | use syntax::codemap::Spanned; |
3157f602 XL |
24 | use syntax::parse::token::{self, keywords}; |
25 | use syntax::visit::{self, Visitor}; | |
26 | use syntax_pos::Span; | |
27 | use errors; | |
28 | ||
29 | struct AstValidator<'a> { | |
30 | session: &'a Session, | |
31 | } | |
32 | ||
33 | impl<'a> AstValidator<'a> { | |
34 | fn err_handler(&self) -> &errors::Handler { | |
35 | &self.session.parse_sess.span_diagnostic | |
36 | } | |
37 | ||
38 | fn check_label(&self, label: Ident, span: Span, id: NodeId) { | |
39 | if label.name == keywords::StaticLifetime.name() { | |
40 | self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name)); | |
41 | } | |
42 | if label.name.as_str() == "'_" { | |
5bcae85e SL |
43 | self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE, |
44 | id, | |
45 | span, | |
46 | format!("invalid label name `{}`", label.name)); | |
3157f602 XL |
47 | } |
48 | } | |
49 | ||
50 | fn invalid_visibility(&self, vis: &Visibility, span: Span, note: Option<&str>) { | |
51 | if vis != &Visibility::Inherited { | |
5bcae85e SL |
52 | let mut err = struct_span_err!(self.session, |
53 | span, | |
54 | E0449, | |
3157f602 | 55 | "unnecessary visibility qualifier"); |
9e0c209e SL |
56 | if vis == &Visibility::Public { |
57 | err.span_label(span, &format!("`pub` not needed here")); | |
58 | } | |
3157f602 | 59 | if let Some(note) = note { |
9e0c209e | 60 | err.note(note); |
3157f602 XL |
61 | } |
62 | err.emit(); | |
63 | } | |
64 | } | |
5bcae85e SL |
65 | |
66 | fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) { | |
67 | for arg in &decl.inputs { | |
68 | match arg.pat.node { | |
69 | PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) | | |
70 | PatKind::Wild => {} | |
71 | PatKind::Ident(..) => report_err(arg.pat.span, true), | |
72 | _ => report_err(arg.pat.span, false), | |
73 | } | |
74 | } | |
75 | } | |
9e0c209e SL |
76 | |
77 | fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) { | |
78 | match constness.node { | |
79 | Constness::Const => { | |
80 | struct_span_err!(self.session, constness.span, E0379, | |
81 | "trait fns cannot be declared const") | |
82 | .span_label(constness.span, &format!("trait fns cannot be const")) | |
83 | .emit(); | |
84 | } | |
85 | _ => {} | |
86 | } | |
87 | } | |
3157f602 XL |
88 | } |
89 | ||
90 | impl<'a> Visitor for AstValidator<'a> { | |
91 | fn visit_lifetime(&mut self, lt: &Lifetime) { | |
92 | if lt.name.as_str() == "'_" { | |
5bcae85e SL |
93 | self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE, |
94 | lt.id, | |
95 | lt.span, | |
96 | format!("invalid lifetime name `{}`", lt.name)); | |
3157f602 XL |
97 | } |
98 | ||
99 | visit::walk_lifetime(self, lt) | |
100 | } | |
101 | ||
102 | fn visit_expr(&mut self, expr: &Expr) { | |
103 | match expr.node { | |
9e0c209e | 104 | ExprKind::While(.., Some(ident)) | |
5bcae85e | 105 | ExprKind::Loop(_, Some(ident)) | |
9e0c209e SL |
106 | ExprKind::WhileLet(.., Some(ident)) | |
107 | ExprKind::ForLoop(.., Some(ident)) | | |
5bcae85e SL |
108 | ExprKind::Break(Some(ident)) | |
109 | ExprKind::Continue(Some(ident)) => { | |
3157f602 XL |
110 | self.check_label(ident.node, ident.span, expr.id); |
111 | } | |
112 | _ => {} | |
113 | } | |
114 | ||
115 | visit::walk_expr(self, expr) | |
116 | } | |
117 | ||
5bcae85e SL |
118 | fn visit_ty(&mut self, ty: &Ty) { |
119 | match ty.node { | |
120 | TyKind::BareFn(ref bfty) => { | |
121 | self.check_decl_no_pat(&bfty.decl, |span, _| { | |
122 | let mut err = struct_span_err!(self.session, | |
123 | span, | |
124 | E0561, | |
125 | "patterns aren't allowed in function pointer \ | |
126 | types"); | |
127 | err.span_note(span, | |
128 | "this is a recent error, see issue #35203 for more details"); | |
129 | err.emit(); | |
130 | }); | |
131 | } | |
132 | _ => {} | |
133 | } | |
134 | ||
135 | visit::walk_ty(self, ty) | |
136 | } | |
137 | ||
3157f602 XL |
138 | fn visit_path(&mut self, path: &Path, id: NodeId) { |
139 | if path.global && path.segments.len() > 0 { | |
140 | let ident = path.segments[0].identifier; | |
141 | if token::Ident(ident).is_path_segment_keyword() { | |
5bcae85e SL |
142 | self.session.add_lint(lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, |
143 | id, | |
144 | path.span, | |
145 | format!("global paths cannot start with `{}`", ident)); | |
3157f602 XL |
146 | } |
147 | } | |
148 | ||
149 | visit::walk_path(self, path) | |
150 | } | |
151 | ||
152 | fn visit_item(&mut self, item: &Item) { | |
153 | match item.node { | |
154 | ItemKind::Use(ref view_path) => { | |
155 | let path = view_path.node.path(); | |
156 | if !path.segments.iter().all(|segment| segment.parameters.is_empty()) { | |
5bcae85e SL |
157 | self.err_handler() |
158 | .span_err(path.span, "type or lifetime parameters in import path"); | |
3157f602 XL |
159 | } |
160 | } | |
9e0c209e | 161 | ItemKind::Impl(.., Some(..), _, ref impl_items) => { |
3157f602 XL |
162 | self.invalid_visibility(&item.vis, item.span, None); |
163 | for impl_item in impl_items { | |
164 | self.invalid_visibility(&impl_item.vis, impl_item.span, None); | |
9e0c209e SL |
165 | if let ImplItemKind::Method(ref sig, _) = impl_item.node { |
166 | self.check_trait_fn_not_const(sig.constness); | |
167 | } | |
3157f602 XL |
168 | } |
169 | } | |
9e0c209e | 170 | ItemKind::Impl(.., None, _, _) => { |
5bcae85e SL |
171 | self.invalid_visibility(&item.vis, |
172 | item.span, | |
173 | Some("place qualifiers on individual impl items instead")); | |
3157f602 XL |
174 | } |
175 | ItemKind::DefaultImpl(..) => { | |
176 | self.invalid_visibility(&item.vis, item.span, None); | |
177 | } | |
178 | ItemKind::ForeignMod(..) => { | |
5bcae85e SL |
179 | self.invalid_visibility(&item.vis, |
180 | item.span, | |
181 | Some("place qualifiers on individual foreign items \ | |
182 | instead")); | |
3157f602 XL |
183 | } |
184 | ItemKind::Enum(ref def, _) => { | |
185 | for variant in &def.variants { | |
186 | for field in variant.node.data.fields() { | |
187 | self.invalid_visibility(&field.vis, field.span, None); | |
188 | } | |
189 | } | |
190 | } | |
9e0c209e SL |
191 | ItemKind::Trait(.., ref trait_items) => { |
192 | for trait_item in trait_items { | |
193 | if let TraitItemKind::Method(ref sig, _) = trait_item.node { | |
194 | self.check_trait_fn_not_const(sig.constness); | |
195 | } | |
196 | } | |
197 | } | |
5bcae85e SL |
198 | ItemKind::Mod(_) => { |
199 | // Ensure that `path` attributes on modules are recorded as used (c.f. #35584). | |
200 | attr::first_attr_value_str_by_name(&item.attrs, "path"); | |
201 | } | |
9e0c209e SL |
202 | ItemKind::Union(ref vdata, _) => { |
203 | if !vdata.is_struct() { | |
204 | self.err_handler().span_err(item.span, | |
205 | "tuple and unit unions are not permitted"); | |
206 | } | |
207 | if vdata.fields().len() == 0 { | |
208 | self.err_handler().span_err(item.span, | |
209 | "unions cannot have zero fields"); | |
210 | } | |
211 | } | |
3157f602 XL |
212 | _ => {} |
213 | } | |
214 | ||
215 | visit::walk_item(self, item) | |
216 | } | |
217 | ||
5bcae85e SL |
218 | fn visit_foreign_item(&mut self, fi: &ForeignItem) { |
219 | match fi.node { | |
220 | ForeignItemKind::Fn(ref decl, _) => { | |
221 | self.check_decl_no_pat(decl, |span, is_recent| { | |
222 | let mut err = struct_span_err!(self.session, | |
223 | span, | |
224 | E0130, | |
225 | "patterns aren't allowed in foreign function \ | |
226 | declarations"); | |
227 | err.span_label(span, &format!("pattern not allowed in foreign function")); | |
228 | if is_recent { | |
229 | err.span_note(span, | |
230 | "this is a recent error, see issue #35203 for more details"); | |
231 | } | |
232 | err.emit(); | |
233 | }); | |
3157f602 | 234 | } |
5bcae85e | 235 | ForeignItemKind::Static(..) => {} |
3157f602 XL |
236 | } |
237 | ||
5bcae85e | 238 | visit::walk_foreign_item(self, fi) |
3157f602 XL |
239 | } |
240 | ||
241 | fn visit_vis(&mut self, vis: &Visibility) { | |
242 | match *vis { | |
5bcae85e | 243 | Visibility::Restricted { ref path, .. } => { |
3157f602 | 244 | if !path.segments.iter().all(|segment| segment.parameters.is_empty()) { |
5bcae85e SL |
245 | self.err_handler() |
246 | .span_err(path.span, "type or lifetime parameters in visibility path"); | |
3157f602 XL |
247 | } |
248 | } | |
249 | _ => {} | |
250 | } | |
251 | ||
252 | visit::walk_vis(self, vis) | |
253 | } | |
254 | } | |
255 | ||
256 | pub fn check_crate(session: &Session, krate: &Crate) { | |
257 | visit::walk_crate(&mut AstValidator { session: session }, krate) | |
258 | } |