]>
Commit | Line | Data |
---|---|---|
dc9dc135 | 1 | // Validate AST before lowering it to HIR. |
3157f602 XL |
2 | // |
3 | // This pass is supposed to catch things that fit into AST data structures, | |
4 | // but not permitted by the language. It runs after expansion when AST is frozen, | |
5 | // so it can check for erroneous constructions produced by syntax extensions. | |
6 | // This pass is supposed to perform only simple checks not requiring name resolution | |
7 | // or type checking or some other kind of complex analysis. | |
8 | ||
ba9703b0 | 9 | use itertools::{Either, Itertools}; |
ba9703b0 | 10 | use rustc_ast::ptr::P; |
74b04a01 XL |
11 | use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; |
12 | use rustc_ast::walk_list; | |
3dfed10e | 13 | use rustc_ast::*; |
74b04a01 | 14 | use rustc_ast_pretty::pprust; |
9fa01778 | 15 | use rustc_data_structures::fx::FxHashMap; |
ba9703b0 | 16 | use rustc_errors::{error_code, pluralize, struct_span_err, Applicability}; |
60c5eb7d | 17 | use rustc_parse::validate_attr; |
dfeec247 | 18 | use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY; |
5869c6ff | 19 | use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer}; |
dfeec247 | 20 | use rustc_session::Session; |
f9f354fc | 21 | use rustc_span::symbol::{kw, sym, Ident}; |
dfeec247 XL |
22 | use rustc_span::Span; |
23 | use std::mem; | |
f9f354fc | 24 | use std::ops::DerefMut; |
74b04a01 XL |
25 | |
26 | const MORE_EXTERN: &str = | |
27 | "for more information, visit https://doc.rust-lang.org/std/keyword.extern.html"; | |
28 | ||
29 | /// Is `self` allowed semantically as the first parameter in an `FnDecl`? | |
30 | enum SelfSemantic { | |
31 | Yes, | |
32 | No, | |
33 | } | |
dfeec247 XL |
34 | |
35 | /// A syntactic context that disallows certain kinds of bounds (e.g., `?Trait` or `?const Trait`). | |
36 | #[derive(Clone, Copy)] | |
37 | enum BoundContext { | |
38 | ImplTrait, | |
39 | TraitBounds, | |
40 | TraitObject, | |
41 | } | |
9fa01778 | 42 | |
dfeec247 XL |
43 | impl BoundContext { |
44 | fn description(&self) -> &'static str { | |
45 | match self { | |
46 | Self::ImplTrait => "`impl Trait`", | |
47 | Self::TraitBounds => "supertraits", | |
48 | Self::TraitObject => "trait objects", | |
49 | } | |
50 | } | |
51 | } | |
3157f602 XL |
52 | |
53 | struct AstValidator<'a> { | |
54 | session: &'a Session, | |
74b04a01 XL |
55 | |
56 | /// The span of the `extern` in an `extern { ... }` block, if any. | |
57 | extern_mod: Option<&'a Item>, | |
58 | ||
59 | /// Are we inside a trait impl? | |
60 | in_trait_impl: bool, | |
61 | ||
9fa01778 | 62 | has_proc_macro_decls: bool, |
9fa01778 | 63 | |
48663c56 XL |
64 | /// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`. |
65 | /// Nested `impl Trait` _is_ allowed in associated type position, | |
dc9dc135 | 66 | /// e.g., `impl Iterator<Item = impl Debug>`. |
60c5eb7d | 67 | outer_impl_trait: Option<Span>, |
9fa01778 | 68 | |
dfeec247 XL |
69 | /// Keeps track of the `BoundContext` as we recurse. |
70 | /// | |
71 | /// This is used to forbid `?const Trait` bounds in, e.g., | |
72 | /// `impl Iterator<Item = Box<dyn ?const Trait>`. | |
73 | bound_context: Option<BoundContext>, | |
74 | ||
48663c56 XL |
75 | /// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item` |
76 | /// or `Foo::Bar<impl Trait>` | |
9fa01778 XL |
77 | is_impl_trait_banned: bool, |
78 | ||
dc9dc135 XL |
79 | /// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in |
80 | /// certain positions. | |
81 | is_assoc_ty_bound_banned: bool, | |
82 | ||
dfeec247 | 83 | lint_buffer: &'a mut LintBuffer, |
3157f602 XL |
84 | } |
85 | ||
86 | impl<'a> AstValidator<'a> { | |
74b04a01 XL |
87 | fn with_in_trait_impl(&mut self, is_in: bool, f: impl FnOnce(&mut Self)) { |
88 | let old = mem::replace(&mut self.in_trait_impl, is_in); | |
89 | f(self); | |
90 | self.in_trait_impl = old; | |
91 | } | |
92 | ||
9fa01778 XL |
93 | fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) { |
94 | let old = mem::replace(&mut self.is_impl_trait_banned, true); | |
95 | f(self); | |
96 | self.is_impl_trait_banned = old; | |
97 | } | |
98 | ||
dc9dc135 XL |
99 | fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) { |
100 | let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true); | |
101 | f(self); | |
102 | self.is_assoc_ty_bound_banned = old; | |
103 | } | |
104 | ||
60c5eb7d | 105 | fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) { |
9fa01778 | 106 | let old = mem::replace(&mut self.outer_impl_trait, outer); |
dfeec247 XL |
107 | if outer.is_some() { |
108 | self.with_bound_context(BoundContext::ImplTrait, |this| f(this)); | |
109 | } else { | |
110 | f(self) | |
111 | } | |
9fa01778 XL |
112 | self.outer_impl_trait = old; |
113 | } | |
114 | ||
dfeec247 XL |
115 | fn with_bound_context(&mut self, ctx: BoundContext, f: impl FnOnce(&mut Self)) { |
116 | let old = self.bound_context.replace(ctx); | |
117 | f(self); | |
118 | self.bound_context = old; | |
119 | } | |
120 | ||
dc9dc135 XL |
121 | fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) { |
122 | match constraint.kind { | |
60c5eb7d | 123 | AssocTyConstraintKind::Equality { .. } => {} |
dc9dc135 XL |
124 | AssocTyConstraintKind::Bound { .. } => { |
125 | if self.is_assoc_ty_bound_banned { | |
dfeec247 XL |
126 | self.err_handler().span_err( |
127 | constraint.span, | |
128 | "associated type bounds are not allowed within structs, enums, or unions", | |
dc9dc135 XL |
129 | ); |
130 | } | |
131 | } | |
9fa01778 | 132 | } |
dc9dc135 | 133 | self.visit_assoc_ty_constraint(constraint); |
9fa01778 XL |
134 | } |
135 | ||
dc9dc135 | 136 | // Mirrors `visit::walk_ty`, but tracks relevant state. |
9fa01778 | 137 | fn walk_ty(&mut self, t: &'a Ty) { |
e74abb32 | 138 | match t.kind { |
9fa01778 | 139 | TyKind::ImplTrait(..) => { |
60c5eb7d | 140 | self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t)) |
9fa01778 | 141 | } |
dfeec247 XL |
142 | TyKind::TraitObject(..) => { |
143 | self.with_bound_context(BoundContext::TraitObject, |this| visit::walk_ty(this, t)); | |
144 | } | |
9fa01778 XL |
145 | TyKind::Path(ref qself, ref path) => { |
146 | // We allow these: | |
147 | // - `Option<impl Trait>` | |
148 | // - `option::Option<impl Trait>` | |
149 | // - `option::Option<T>::Foo<impl Trait> | |
150 | // | |
151 | // But not these: | |
152 | // - `<impl Trait>::Foo` | |
153 | // - `option::Option<impl Trait>::Foo`. | |
154 | // | |
155 | // To implement this, we disallow `impl Trait` from `qself` | |
156 | // (for cases like `<impl Trait>::Foo>`) | |
157 | // but we allow `impl Trait` in `GenericArgs` | |
158 | // iff there are no more PathSegments. | |
159 | if let Some(ref qself) = *qself { | |
160 | // `impl Trait` in `qself` is always illegal | |
161 | self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty)); | |
162 | } | |
163 | ||
164 | // Note that there should be a call to visit_path here, | |
165 | // so if any logic is added to process `Path`s a call to it should be | |
166 | // added both in visit_path and here. This code mirrors visit::walk_path. | |
167 | for (i, segment) in path.segments.iter().enumerate() { | |
168 | // Allow `impl Trait` iff we're on the final path segment | |
169 | if i == path.segments.len() - 1 { | |
170 | self.visit_path_segment(path.span, segment); | |
171 | } else { | |
172 | self.with_banned_impl_trait(|this| { | |
173 | this.visit_path_segment(path.span, segment) | |
174 | }); | |
175 | } | |
176 | } | |
177 | } | |
178 | _ => visit::walk_ty(self, t), | |
179 | } | |
180 | } | |
181 | ||
dfeec247 | 182 | fn err_handler(&self) -> &rustc_errors::Handler { |
b7449926 | 183 | &self.session.diagnostic() |
3157f602 XL |
184 | } |
185 | ||
83c7162d | 186 | fn check_lifetime(&self, ident: Ident) { |
5869c6ff | 187 | let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Empty]; |
94b46f34 | 188 | if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() { |
83c7162d | 189 | self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names"); |
ff7c6d11 XL |
190 | } |
191 | } | |
192 | ||
83c7162d | 193 | fn check_label(&self, ident: Ident) { |
94b46f34 | 194 | if ident.without_first_quote().is_reserved() { |
83c7162d XL |
195 | self.err_handler() |
196 | .span_err(ident.span, &format!("invalid label name `{}`", ident.name)); | |
3157f602 | 197 | } |
3157f602 XL |
198 | } |
199 | ||
0531ce1d | 200 | fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) { |
1b1a35ee | 201 | if let VisibilityKind::Inherited = vis.kind { |
dfeec247 | 202 | return; |
8faf50e0 XL |
203 | } |
204 | ||
dfeec247 XL |
205 | let mut err = |
206 | struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier"); | |
1b1a35ee | 207 | if vis.kind.is_pub() { |
8faf50e0 | 208 | err.span_label(vis.span, "`pub` not permitted here because it's implied"); |
3157f602 | 209 | } |
8faf50e0 XL |
210 | if let Some(note) = note { |
211 | err.note(note); | |
212 | } | |
213 | err.emit(); | |
3157f602 | 214 | } |
5bcae85e | 215 | |
5869c6ff | 216 | fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) { |
60c5eb7d XL |
217 | for Param { pat, .. } in &decl.inputs { |
218 | match pat.kind { | |
dfeec247 | 219 | PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {} |
5869c6ff XL |
220 | PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => { |
221 | report_err(pat.span, Some(ident), true) | |
dfeec247 | 222 | } |
5869c6ff | 223 | _ => report_err(pat.span, None, false), |
5bcae85e SL |
224 | } |
225 | } | |
226 | } | |
9e0c209e | 227 | |
74b04a01 XL |
228 | fn check_trait_fn_not_async(&self, fn_span: Span, asyncness: Async) { |
229 | if let Async::Yes { span, .. } = asyncness { | |
230 | struct_span_err!( | |
231 | self.session, | |
232 | fn_span, | |
233 | E0706, | |
234 | "functions in traits cannot be declared `async`" | |
235 | ) | |
236 | .span_label(span, "`async` because of this") | |
237 | .note("`async` trait functions are not currently supported") | |
238 | .note("consider using the `async-trait` crate: https://crates.io/crates/async-trait") | |
239 | .emit(); | |
8faf50e0 XL |
240 | } |
241 | } | |
242 | ||
74b04a01 XL |
243 | fn check_trait_fn_not_const(&self, constness: Const) { |
244 | if let Const::Yes(span) = constness { | |
dfeec247 XL |
245 | struct_span_err!( |
246 | self.session, | |
74b04a01 | 247 | span, |
dfeec247 | 248 | E0379, |
74b04a01 | 249 | "functions in traits cannot be declared const" |
dfeec247 | 250 | ) |
74b04a01 | 251 | .span_label(span, "functions in traits cannot be const") |
dfeec247 | 252 | .emit(); |
9e0c209e SL |
253 | } |
254 | } | |
476ff2be | 255 | |
dfeec247 | 256 | // FIXME(ecstaticmorse): Instead, use `bound_context` to check this in `visit_param_bound`. |
8faf50e0 | 257 | fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) { |
476ff2be | 258 | for bound in bounds { |
8faf50e0 | 259 | if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound { |
dfeec247 XL |
260 | let mut err = self.err_handler().struct_span_err( |
261 | poly.span, | |
262 | &format!("`?Trait` is not permitted in {}", where_), | |
263 | ); | |
476ff2be | 264 | if is_trait { |
e74abb32 XL |
265 | let path_str = pprust::path_to_string(&poly.trait_ref.path); |
266 | err.note(&format!("traits are `?{}` by default", path_str)); | |
476ff2be SL |
267 | } |
268 | err.emit(); | |
269 | } | |
270 | } | |
271 | } | |
041b39d2 | 272 | |
9fa01778 XL |
273 | /// Matches `'-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus)`, |
274 | /// or paths for ranges. | |
275 | // | |
276 | // FIXME: do we want to allow `expr -> pattern` conversion to create path expressions? | |
277 | // That means making this work: | |
278 | // | |
279 | // ```rust,ignore (FIXME) | |
280 | // struct S; | |
281 | // macro_rules! m { | |
282 | // ($a:expr) => { | |
283 | // let $a = S; | |
284 | // } | |
285 | // } | |
286 | // m!(S); | |
287 | // ``` | |
041b39d2 | 288 | fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) { |
e74abb32 | 289 | match expr.kind { |
29967ef6 | 290 | ExprKind::Lit(..) | ExprKind::ConstBlock(..) | ExprKind::Err => {} |
041b39d2 | 291 | ExprKind::Path(..) if allow_paths => {} |
ba9703b0 | 292 | ExprKind::Unary(UnOp::Neg, ref inner) if matches!(inner.kind, ExprKind::Lit(_)) => {} |
dfeec247 XL |
293 | _ => self.err_handler().span_err( |
294 | expr.span, | |
295 | "arbitrary expressions aren't allowed \ | |
296 | in patterns", | |
297 | ), | |
041b39d2 XL |
298 | } |
299 | } | |
0531ce1d | 300 | |
8faf50e0 XL |
301 | fn check_late_bound_lifetime_defs(&self, params: &[GenericParam]) { |
302 | // Check only lifetime parameters are present and that the lifetime | |
303 | // parameters that are present have no bounds. | |
dfeec247 XL |
304 | let non_lt_param_spans: Vec<_> = params |
305 | .iter() | |
306 | .filter_map(|param| match param.kind { | |
307 | GenericParamKind::Lifetime { .. } => { | |
308 | if !param.bounds.is_empty() { | |
309 | let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect(); | |
310 | self.err_handler() | |
311 | .span_err(spans, "lifetime bounds cannot be used in this context"); | |
312 | } | |
313 | None | |
0531ce1d | 314 | } |
dfeec247 XL |
315 | _ => Some(param.ident.span), |
316 | }) | |
317 | .collect(); | |
8faf50e0 | 318 | if !non_lt_param_spans.is_empty() { |
dfeec247 XL |
319 | self.err_handler().span_err( |
320 | non_lt_param_spans, | |
321 | "only lifetime parameters can be used in this context", | |
322 | ); | |
0531ce1d XL |
323 | } |
324 | } | |
b7449926 | 325 | |
74b04a01 XL |
326 | fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) { |
327 | self.check_decl_cvaradic_pos(fn_decl); | |
328 | self.check_decl_attrs(fn_decl); | |
329 | self.check_decl_self_param(fn_decl, self_semantic); | |
330 | } | |
331 | ||
332 | fn check_decl_cvaradic_pos(&self, fn_decl: &FnDecl) { | |
dfeec247 XL |
333 | match &*fn_decl.inputs { |
334 | [Param { ty, span, .. }] => { | |
335 | if let TyKind::CVarArgs = ty.kind { | |
336 | self.err_handler().span_err( | |
337 | *span, | |
338 | "C-variadic function must be declared with at least one named argument", | |
339 | ); | |
340 | } | |
341 | } | |
342 | [ps @ .., _] => { | |
343 | for Param { ty, span, .. } in ps { | |
344 | if let TyKind::CVarArgs = ty.kind { | |
345 | self.err_handler().span_err( | |
346 | *span, | |
347 | "`...` must be the last argument of a C-variadic function", | |
348 | ); | |
349 | } | |
350 | } | |
351 | } | |
352 | _ => {} | |
353 | } | |
74b04a01 | 354 | } |
dfeec247 | 355 | |
74b04a01 | 356 | fn check_decl_attrs(&self, fn_decl: &FnDecl) { |
dc9dc135 XL |
357 | fn_decl |
358 | .inputs | |
359 | .iter() | |
360 | .flat_map(|i| i.attrs.as_ref()) | |
361 | .filter(|attr| { | |
362 | let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn]; | |
74b04a01 | 363 | !arr.contains(&attr.name_or_empty()) && rustc_attr::is_builtin_attr(attr) |
dc9dc135 | 364 | }) |
dfeec247 XL |
365 | .for_each(|attr| { |
366 | if attr.is_doc_comment() { | |
367 | self.err_handler() | |
368 | .struct_span_err( | |
369 | attr.span, | |
370 | "documentation comments cannot be applied to function parameters", | |
371 | ) | |
372 | .span_label(attr.span, "doc comments are not allowed here") | |
373 | .emit(); | |
374 | } else { | |
375 | self.err_handler().span_err( | |
376 | attr.span, | |
377 | "allow, cfg, cfg_attr, deny, \ | |
378 | forbid, and warn are the only allowed built-in attributes in function parameters", | |
379 | ) | |
380 | } | |
dc9dc135 | 381 | }); |
b7449926 | 382 | } |
dfeec247 | 383 | |
74b04a01 XL |
384 | fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) { |
385 | if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) { | |
386 | if param.is_self() { | |
387 | self.err_handler() | |
388 | .struct_span_err( | |
389 | param.span, | |
390 | "`self` parameter is only allowed in associated functions", | |
391 | ) | |
392 | .span_label(param.span, "not semantically valid as function parameter") | |
393 | .note("associated functions are those in `impl` or `trait` definitions") | |
394 | .emit(); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
dfeec247 | 399 | fn check_defaultness(&self, span: Span, defaultness: Defaultness) { |
74b04a01 | 400 | if let Defaultness::Default(def_span) = defaultness { |
ba9703b0 | 401 | let span = self.session.source_map().guess_head_span(span); |
dfeec247 | 402 | self.err_handler() |
fc512014 | 403 | .struct_span_err(span, "`default` is only allowed on items in trait impls") |
74b04a01 | 404 | .span_label(def_span, "`default` because of this") |
dfeec247 XL |
405 | .emit(); |
406 | } | |
407 | } | |
408 | ||
74b04a01 | 409 | fn error_item_without_body(&self, sp: Span, ctx: &str, msg: &str, sugg: &str) { |
dfeec247 | 410 | self.err_handler() |
74b04a01 | 411 | .struct_span_err(sp, msg) |
dfeec247 XL |
412 | .span_suggestion( |
413 | self.session.source_map().end_point(sp), | |
414 | &format!("provide a definition for the {}", ctx), | |
415 | sugg.to_string(), | |
416 | Applicability::HasPlaceholders, | |
417 | ) | |
418 | .emit(); | |
419 | } | |
420 | ||
74b04a01 XL |
421 | fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) { |
422 | if body.is_none() { | |
423 | let msg = format!("associated {} in `impl` without body", ctx); | |
424 | self.error_item_without_body(sp, ctx, &msg, sugg); | |
425 | } | |
426 | } | |
427 | ||
428 | fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) { | |
dfeec247 XL |
429 | let span = match bounds { |
430 | [] => return, | |
431 | [b0] => b0.span(), | |
432 | [b0, .., bl] => b0.span().to(bl.span()), | |
433 | }; | |
434 | self.err_handler() | |
74b04a01 | 435 | .struct_span_err(span, &format!("bounds on `type`s in {} have no effect", ctx)) |
dfeec247 XL |
436 | .emit(); |
437 | } | |
438 | ||
74b04a01 XL |
439 | fn check_foreign_ty_genericless(&self, generics: &Generics) { |
440 | let cannot_have = |span, descr, remove_descr| { | |
441 | self.err_handler() | |
442 | .struct_span_err( | |
443 | span, | |
444 | &format!("`type`s inside `extern` blocks cannot have {}", descr), | |
445 | ) | |
446 | .span_suggestion( | |
447 | span, | |
448 | &format!("remove the {}", remove_descr), | |
449 | String::new(), | |
450 | Applicability::MaybeIncorrect, | |
451 | ) | |
452 | .span_label(self.current_extern_span(), "`extern` block begins here") | |
453 | .note(MORE_EXTERN) | |
454 | .emit(); | |
455 | }; | |
456 | ||
457 | if !generics.params.is_empty() { | |
458 | cannot_have(generics.span, "generic parameters", "generic parameters"); | |
459 | } | |
460 | ||
461 | if !generics.where_clause.predicates.is_empty() { | |
462 | cannot_have(generics.where_clause.span, "`where` clauses", "`where` clause"); | |
463 | } | |
464 | } | |
465 | ||
466 | fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) { | |
467 | let body = match body { | |
468 | None => return, | |
469 | Some(body) => body, | |
470 | }; | |
471 | self.err_handler() | |
472 | .struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind)) | |
473 | .span_label(ident.span, "cannot have a body") | |
474 | .span_label(body, "the invalid body") | |
475 | .span_label( | |
476 | self.current_extern_span(), | |
477 | format!( | |
478 | "`extern` blocks define existing foreign {0}s and {0}s \ | |
479 | inside of them cannot have a body", | |
480 | kind | |
481 | ), | |
482 | ) | |
483 | .note(MORE_EXTERN) | |
484 | .emit(); | |
485 | } | |
486 | ||
487 | /// An `fn` in `extern { ... }` cannot have a body `{ ... }`. | |
488 | fn check_foreign_fn_bodyless(&self, ident: Ident, body: Option<&Block>) { | |
489 | let body = match body { | |
490 | None => return, | |
491 | Some(body) => body, | |
492 | }; | |
493 | self.err_handler() | |
494 | .struct_span_err(ident.span, "incorrect function inside `extern` block") | |
495 | .span_label(ident.span, "cannot have a body") | |
496 | .span_suggestion( | |
497 | body.span, | |
498 | "remove the invalid body", | |
499 | ";".to_string(), | |
500 | Applicability::MaybeIncorrect, | |
501 | ) | |
502 | .help( | |
503 | "you might have meant to write a function accessible through FFI, \ | |
504 | which can be done by writing `extern fn` outside of the `extern` block", | |
505 | ) | |
506 | .span_label( | |
507 | self.current_extern_span(), | |
508 | "`extern` blocks define existing foreign functions and functions \ | |
509 | inside of them cannot have a body", | |
510 | ) | |
511 | .note(MORE_EXTERN) | |
512 | .emit(); | |
513 | } | |
514 | ||
515 | fn current_extern_span(&self) -> Span { | |
ba9703b0 | 516 | self.session.source_map().guess_head_span(self.extern_mod.unwrap().span) |
74b04a01 XL |
517 | } |
518 | ||
29967ef6 | 519 | /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`. |
74b04a01 XL |
520 | fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) { |
521 | if header.has_qualifiers() { | |
522 | self.err_handler() | |
523 | .struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers") | |
524 | .span_label(self.current_extern_span(), "in this `extern` block") | |
fc512014 | 525 | .span_suggestion_verbose( |
74b04a01 XL |
526 | span.until(ident.span.shrink_to_lo()), |
527 | "remove the qualifiers", | |
528 | "fn ".to_string(), | |
529 | Applicability::MaybeIncorrect, | |
530 | ) | |
531 | .emit(); | |
532 | } | |
533 | } | |
534 | ||
cdc7bbd5 XL |
535 | /// An item in `extern { ... }` cannot use non-ascii identifier. |
536 | fn check_foreign_item_ascii_only(&self, ident: Ident) { | |
537 | let symbol_str = ident.as_str(); | |
538 | if !symbol_str.is_ascii() { | |
539 | let n = 83942; | |
540 | self.err_handler() | |
541 | .struct_span_err( | |
542 | ident.span, | |
543 | "items in `extern` blocks cannot use non-ascii identifiers", | |
544 | ) | |
545 | .span_label(self.current_extern_span(), "in this `extern` block") | |
546 | .note(&format!( | |
547 | "This limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information", | |
548 | n, n, | |
549 | )) | |
550 | .emit(); | |
551 | } | |
552 | } | |
553 | ||
74b04a01 XL |
554 | /// Reject C-varadic type unless the function is foreign, |
555 | /// or free and `unsafe extern "C"` semantically. | |
556 | fn check_c_varadic_type(&self, fk: FnKind<'a>) { | |
557 | match (fk.ctxt(), fk.header()) { | |
558 | (Some(FnCtxt::Foreign), _) => return, | |
559 | (Some(FnCtxt::Free), Some(header)) => match header.ext { | |
560 | Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }) | Extern::Implicit | |
561 | if matches!(header.unsafety, Unsafe::Yes(_)) => | |
562 | { | |
563 | return; | |
564 | } | |
565 | _ => {} | |
566 | }, | |
567 | _ => {} | |
568 | }; | |
569 | ||
570 | for Param { ty, span, .. } in &fk.decl().inputs { | |
dfeec247 XL |
571 | if let TyKind::CVarArgs = ty.kind { |
572 | self.err_handler() | |
573 | .struct_span_err( | |
574 | *span, | |
575 | "only foreign or `unsafe extern \"C\" functions may be C-variadic", | |
576 | ) | |
577 | .emit(); | |
578 | } | |
579 | } | |
580 | } | |
9fa01778 | 581 | |
74b04a01 XL |
582 | fn check_item_named(&self, ident: Ident, kind: &str) { |
583 | if ident.name != kw::Underscore { | |
584 | return; | |
585 | } | |
586 | self.err_handler() | |
587 | .struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind)) | |
588 | .span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind)) | |
589 | .emit(); | |
590 | } | |
ba9703b0 | 591 | |
f9f354fc XL |
592 | fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) { |
593 | if ident.name.as_str().is_ascii() { | |
594 | return; | |
595 | } | |
596 | let head_span = self.session.source_map().guess_head_span(item_span); | |
597 | struct_span_err!( | |
598 | self.session, | |
599 | head_span, | |
600 | E0754, | |
601 | "`#[no_mangle]` requires ASCII identifier" | |
602 | ) | |
603 | .emit(); | |
604 | } | |
605 | ||
606 | fn check_mod_file_item_asciionly(&self, ident: Ident) { | |
607 | if ident.name.as_str().is_ascii() { | |
608 | return; | |
609 | } | |
610 | struct_span_err!( | |
611 | self.session, | |
612 | ident.span, | |
613 | E0754, | |
cdc7bbd5 | 614 | "trying to load file for module `{}` with non-ascii identifier name", |
f9f354fc XL |
615 | ident.name |
616 | ) | |
617 | .help("consider using `#[path]` attribute to specify filesystem path") | |
618 | .emit(); | |
619 | } | |
620 | ||
ba9703b0 XL |
621 | fn deny_generic_params(&self, generics: &Generics, ident_span: Span) { |
622 | if !generics.params.is_empty() { | |
623 | struct_span_err!( | |
624 | self.session, | |
625 | generics.span, | |
626 | E0567, | |
627 | "auto traits cannot have generic parameters" | |
628 | ) | |
629 | .span_label(ident_span, "auto trait cannot have generic parameters") | |
630 | .span_suggestion( | |
631 | generics.span, | |
632 | "remove the parameters", | |
633 | String::new(), | |
634 | Applicability::MachineApplicable, | |
635 | ) | |
636 | .emit(); | |
637 | } | |
638 | } | |
639 | ||
640 | fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) { | |
641 | if let [first @ last] | [first, .., last] = &bounds[..] { | |
642 | let span = first.span().to(last.span()); | |
643 | struct_span_err!(self.session, span, E0568, "auto traits cannot have super traits") | |
644 | .span_label(ident_span, "auto trait cannot have super traits") | |
645 | .span_suggestion( | |
646 | span, | |
647 | "remove the super traits", | |
648 | String::new(), | |
649 | Applicability::MachineApplicable, | |
650 | ) | |
651 | .emit(); | |
652 | } | |
653 | } | |
654 | ||
655 | fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) { | |
656 | if !trait_items.is_empty() { | |
657 | let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect(); | |
658 | struct_span_err!( | |
659 | self.session, | |
660 | spans, | |
661 | E0380, | |
662 | "auto traits cannot have methods or associated items" | |
663 | ) | |
664 | .span_label(ident_span, "auto trait cannot have items") | |
665 | .emit(); | |
666 | } | |
667 | } | |
668 | ||
669 | fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String { | |
670 | // Lifetimes always come first. | |
671 | let lt_sugg = data.args.iter().filter_map(|arg| match arg { | |
672 | AngleBracketedArg::Arg(lt @ GenericArg::Lifetime(_)) => { | |
673 | Some(pprust::to_string(|s| s.print_generic_arg(lt))) | |
674 | } | |
675 | _ => None, | |
676 | }); | |
677 | let args_sugg = data.args.iter().filter_map(|a| match a { | |
678 | AngleBracketedArg::Arg(GenericArg::Lifetime(_)) | AngleBracketedArg::Constraint(_) => { | |
679 | None | |
680 | } | |
681 | AngleBracketedArg::Arg(arg) => Some(pprust::to_string(|s| s.print_generic_arg(arg))), | |
682 | }); | |
683 | // Constraints always come last. | |
684 | let constraint_sugg = data.args.iter().filter_map(|a| match a { | |
685 | AngleBracketedArg::Arg(_) => None, | |
686 | AngleBracketedArg::Constraint(c) => { | |
687 | Some(pprust::to_string(|s| s.print_assoc_constraint(c))) | |
688 | } | |
689 | }); | |
690 | format!( | |
691 | "<{}>", | |
692 | lt_sugg.chain(args_sugg).chain(constraint_sugg).collect::<Vec<String>>().join(", ") | |
693 | ) | |
694 | } | |
695 | ||
696 | /// Enforce generic args coming before constraints in `<...>` of a path segment. | |
697 | fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) { | |
698 | // Early exit in case it's partitioned as it should be. | |
699 | if data.args.iter().is_partitioned(|arg| matches!(arg, AngleBracketedArg::Arg(_))) { | |
700 | return; | |
701 | } | |
702 | // Find all generic argument coming after the first constraint... | |
703 | let (constraint_spans, arg_spans): (Vec<Span>, Vec<Span>) = | |
704 | data.args.iter().partition_map(|arg| match arg { | |
705 | AngleBracketedArg::Constraint(c) => Either::Left(c.span), | |
706 | AngleBracketedArg::Arg(a) => Either::Right(a.span()), | |
707 | }); | |
708 | let args_len = arg_spans.len(); | |
709 | let constraint_len = constraint_spans.len(); | |
710 | // ...and then error: | |
711 | self.err_handler() | |
712 | .struct_span_err( | |
713 | arg_spans.clone(), | |
714 | "generic arguments must come before the first constraint", | |
715 | ) | |
716 | .span_label(constraint_spans[0], &format!("constraint{}", pluralize!(constraint_len))) | |
717 | .span_label( | |
718 | *arg_spans.iter().last().unwrap(), | |
719 | &format!("generic argument{}", pluralize!(args_len)), | |
720 | ) | |
721 | .span_labels(constraint_spans, "") | |
722 | .span_labels(arg_spans, "") | |
723 | .span_suggestion_verbose( | |
724 | data.span, | |
725 | &format!( | |
726 | "move the constraint{} after the generic argument{}", | |
727 | pluralize!(constraint_len), | |
728 | pluralize!(args_len) | |
729 | ), | |
730 | self.correct_generic_order_suggestion(&data), | |
731 | Applicability::MachineApplicable, | |
732 | ) | |
733 | .emit(); | |
734 | } | |
9fa01778 | 735 | } |
b7449926 | 736 | |
ba9703b0 XL |
737 | /// Checks that generic parameters are in the correct order, |
738 | /// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`) | |
5869c6ff | 739 | fn validate_generic_param_order( |
532ac7d7 | 740 | sess: &Session, |
dfeec247 | 741 | handler: &rustc_errors::Handler, |
5869c6ff | 742 | generics: &[GenericParam], |
9fa01778 XL |
743 | span: Span, |
744 | ) { | |
745 | let mut max_param: Option<ParamKindOrd> = None; | |
746 | let mut out_of_order = FxHashMap::default(); | |
747 | let mut param_idents = vec![]; | |
748 | ||
5869c6ff XL |
749 | for param in generics { |
750 | let ident = Some(param.ident.to_string()); | |
751 | let (kind, bounds, span) = (¶m.kind, Some(&*param.bounds), param.ident.span); | |
752 | let (ord_kind, ident) = match ¶m.kind { | |
753 | GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident), | |
754 | GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident), | |
755 | GenericParamKind::Const { ref ty, kw_span: _, default: _ } => { | |
756 | let ty = pprust::ty_to_string(ty); | |
cdc7bbd5 | 757 | let unordered = sess.features_untracked().unordered_const_ty_params(); |
5869c6ff XL |
758 | (ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty))) |
759 | } | |
760 | }; | |
9fa01778 | 761 | if let Some(ident) = ident { |
5869c6ff | 762 | param_idents.push((kind, ord_kind, bounds, param_idents.len(), ident)); |
9fa01778 XL |
763 | } |
764 | let max_param = &mut max_param; | |
765 | match max_param { | |
5869c6ff XL |
766 | Some(max_param) if *max_param > ord_kind => { |
767 | let entry = out_of_order.entry(ord_kind).or_insert((*max_param, vec![])); | |
9fa01778 XL |
768 | entry.1.push(span); |
769 | } | |
5869c6ff | 770 | Some(_) | None => *max_param = Some(ord_kind), |
9fa01778 XL |
771 | }; |
772 | } | |
773 | ||
774 | let mut ordered_params = "<".to_string(); | |
775 | if !out_of_order.is_empty() { | |
5869c6ff | 776 | param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i)); |
9fa01778 | 777 | let mut first = true; |
5869c6ff | 778 | for (kind, _, bounds, _, ident) in param_idents { |
9fa01778 XL |
779 | if !first { |
780 | ordered_params += ", "; | |
781 | } | |
782 | ordered_params += &ident; | |
532ac7d7 XL |
783 | if let Some(bounds) = bounds { |
784 | if !bounds.is_empty() { | |
785 | ordered_params += ": "; | |
786 | ordered_params += &pprust::bounds_to_string(&bounds); | |
787 | } | |
788 | } | |
5869c6ff XL |
789 | match kind { |
790 | GenericParamKind::Type { default: Some(default) } => { | |
791 | ordered_params += " = "; | |
792 | ordered_params += &pprust::ty_to_string(default); | |
793 | } | |
794 | GenericParamKind::Type { default: None } => (), | |
795 | GenericParamKind::Lifetime => (), | |
796 | // FIXME(const_generics_defaults) | |
797 | GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (), | |
798 | } | |
9fa01778 XL |
799 | first = false; |
800 | } | |
801 | } | |
802 | ordered_params += ">"; | |
803 | ||
48663c56 | 804 | for (param_ord, (max_param, spans)) in &out_of_order { |
74b04a01 XL |
805 | let mut err = |
806 | handler.struct_span_err( | |
807 | spans.clone(), | |
532ac7d7 | 808 | &format!( |
74b04a01 XL |
809 | "{} parameters must be declared prior to {} parameters", |
810 | param_ord, max_param, | |
532ac7d7 | 811 | ), |
9fa01778 | 812 | ); |
74b04a01 XL |
813 | err.span_suggestion( |
814 | span, | |
815 | &format!( | |
5869c6ff | 816 | "reorder the parameters: lifetimes, {}", |
3dfed10e | 817 | if sess.features_untracked().const_generics { |
5869c6ff | 818 | "then consts and types" |
3dfed10e | 819 | } else { |
5869c6ff XL |
820 | "then types, then consts" |
821 | } | |
74b04a01 XL |
822 | ), |
823 | ordered_params.clone(), | |
824 | Applicability::MachineApplicable, | |
825 | ); | |
9fa01778 XL |
826 | err.emit(); |
827 | } | |
3157f602 XL |
828 | } |
829 | ||
476ff2be | 830 | impl<'a> Visitor<'a> for AstValidator<'a> { |
60c5eb7d XL |
831 | fn visit_attribute(&mut self, attr: &Attribute) { |
832 | validate_attr::check_meta(&self.session.parse_sess, attr); | |
833 | } | |
834 | ||
476ff2be | 835 | fn visit_expr(&mut self, expr: &'a Expr) { |
e74abb32 | 836 | match &expr.kind { |
29967ef6 | 837 | ExprKind::LlvmInlineAsm(..) if !self.session.target.allow_asm => { |
dfeec247 XL |
838 | struct_span_err!( |
839 | self.session, | |
840 | expr.span, | |
841 | E0472, | |
ba9703b0 | 842 | "llvm_asm! is unsupported on this target" |
dfeec247 XL |
843 | ) |
844 | .emit(); | |
3157f602 XL |
845 | } |
846 | _ => {} | |
847 | } | |
848 | ||
dc9dc135 | 849 | visit::walk_expr(self, expr); |
3157f602 XL |
850 | } |
851 | ||
476ff2be | 852 | fn visit_ty(&mut self, ty: &'a Ty) { |
e74abb32 | 853 | match ty.kind { |
5bcae85e | 854 | TyKind::BareFn(ref bfty) => { |
74b04a01 | 855 | self.check_fn_decl(&bfty.decl, SelfSemantic::No); |
5869c6ff | 856 | Self::check_decl_no_pat(&bfty.decl, |span, _, _| { |
dfeec247 XL |
857 | struct_span_err!( |
858 | self.session, | |
859 | span, | |
860 | E0561, | |
861 | "patterns aren't allowed in function pointer types" | |
862 | ) | |
863 | .emit(); | |
5bcae85e | 864 | }); |
0531ce1d | 865 | self.check_late_bound_lifetime_defs(&bfty.generic_params); |
5bcae85e | 866 | } |
abe05a73 | 867 | TyKind::TraitObject(ref bounds, ..) => { |
32a655c1 SL |
868 | let mut any_lifetime_bounds = false; |
869 | for bound in bounds { | |
8faf50e0 | 870 | if let GenericBound::Outlives(ref lifetime) = *bound { |
32a655c1 | 871 | if any_lifetime_bounds { |
dfeec247 XL |
872 | struct_span_err!( |
873 | self.session, | |
874 | lifetime.ident.span, | |
875 | E0226, | |
876 | "only a single explicit lifetime bound is permitted" | |
877 | ) | |
878 | .emit(); | |
32a655c1 SL |
879 | break; |
880 | } | |
881 | any_lifetime_bounds = true; | |
882 | } | |
883 | } | |
476ff2be SL |
884 | self.no_questions_in_bounds(bounds, "trait object types", false); |
885 | } | |
8faf50e0 | 886 | TyKind::ImplTrait(_, ref bounds) => { |
9fa01778 | 887 | if self.is_impl_trait_banned { |
60c5eb7d | 888 | struct_span_err!( |
dfeec247 XL |
889 | self.session, |
890 | ty.span, | |
891 | E0667, | |
60c5eb7d XL |
892 | "`impl Trait` is not allowed in path parameters" |
893 | ) | |
894 | .emit(); | |
9fa01778 XL |
895 | } |
896 | ||
60c5eb7d XL |
897 | if let Some(outer_impl_trait_sp) = self.outer_impl_trait { |
898 | struct_span_err!( | |
dfeec247 XL |
899 | self.session, |
900 | ty.span, | |
901 | E0666, | |
60c5eb7d XL |
902 | "nested `impl Trait` is not allowed" |
903 | ) | |
904 | .span_label(outer_impl_trait_sp, "outer `impl Trait`") | |
905 | .span_label(ty.span, "nested `impl Trait` here") | |
906 | .emit(); | |
9fa01778 XL |
907 | } |
908 | ||
1b1a35ee | 909 | if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) { |
32a655c1 SL |
910 | self.err_handler().span_err(ty.span, "at least one trait must be specified"); |
911 | } | |
9fa01778 | 912 | |
60c5eb7d | 913 | self.walk_ty(ty); |
9fa01778 | 914 | return; |
32a655c1 | 915 | } |
5bcae85e SL |
916 | _ => {} |
917 | } | |
918 | ||
9fa01778 | 919 | self.walk_ty(ty) |
5bcae85e SL |
920 | } |
921 | ||
2c00a5a8 | 922 | fn visit_label(&mut self, label: &'a Label) { |
83c7162d | 923 | self.check_label(label.ident); |
2c00a5a8 XL |
924 | visit::walk_label(self, label); |
925 | } | |
926 | ||
ff7c6d11 | 927 | fn visit_lifetime(&mut self, lifetime: &'a Lifetime) { |
83c7162d | 928 | self.check_lifetime(lifetime.ident); |
ff7c6d11 | 929 | visit::walk_lifetime(self, lifetime); |
3157f602 XL |
930 | } |
931 | ||
476ff2be | 932 | fn visit_item(&mut self, item: &'a Item) { |
3dfed10e | 933 | if item.attrs.iter().any(|attr| self.session.is_proc_macro_attr(attr)) { |
9fa01778 XL |
934 | self.has_proc_macro_decls = true; |
935 | } | |
936 | ||
3dfed10e | 937 | if self.session.contains_name(&item.attrs, sym::no_mangle) { |
f9f354fc XL |
938 | self.check_nomangle_item_asciionly(item.ident, item.span); |
939 | } | |
940 | ||
e74abb32 | 941 | match item.kind { |
5869c6ff | 942 | ItemKind::Impl(box ImplKind { |
dfeec247 XL |
943 | unsafety, |
944 | polarity, | |
945 | defaultness: _, | |
946 | constness: _, | |
947 | generics: _, | |
ba9703b0 | 948 | of_trait: Some(ref t), |
dfeec247 | 949 | ref self_ty, |
74b04a01 | 950 | items: _, |
5869c6ff | 951 | }) => { |
74b04a01 XL |
952 | self.with_in_trait_impl(true, |this| { |
953 | this.invalid_visibility(&item.vis, None); | |
954 | if let TyKind::Err = self_ty.kind { | |
955 | this.err_handler() | |
956 | .struct_span_err( | |
957 | item.span, | |
958 | "`impl Trait for .. {}` is an obsolete syntax", | |
959 | ) | |
960 | .help("use `auto trait Trait {}` instead") | |
961 | .emit(); | |
962 | } | |
ba9703b0 | 963 | if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) { |
74b04a01 XL |
964 | struct_span_err!( |
965 | this.session, | |
ba9703b0 | 966 | sp.to(t.path.span), |
74b04a01 XL |
967 | E0198, |
968 | "negative impls cannot be unsafe" | |
969 | ) | |
ba9703b0 | 970 | .span_label(sp, "negative because of this") |
74b04a01 | 971 | .span_label(span, "unsafe because of this") |
dfeec247 | 972 | .emit(); |
9e0c209e | 973 | } |
74b04a01 XL |
974 | |
975 | visit::walk_item(this, item); | |
976 | }); | |
977 | return; // Avoid visiting again. | |
3157f602 | 978 | } |
5869c6ff | 979 | ItemKind::Impl(box ImplKind { |
dfeec247 XL |
980 | unsafety, |
981 | polarity, | |
982 | defaultness, | |
983 | constness, | |
984 | generics: _, | |
985 | of_trait: None, | |
ba9703b0 | 986 | ref self_ty, |
dfeec247 | 987 | items: _, |
5869c6ff | 988 | }) => { |
ba9703b0 XL |
989 | let error = |annotation_span, annotation| { |
990 | let mut err = self.err_handler().struct_span_err( | |
991 | self_ty.span, | |
992 | &format!("inherent impls cannot be {}", annotation), | |
993 | ); | |
994 | err.span_label(annotation_span, &format!("{} because of this", annotation)); | |
995 | err.span_label(self_ty.span, "inherent impl for this type"); | |
996 | err | |
997 | }; | |
998 | ||
dfeec247 XL |
999 | self.invalid_visibility( |
1000 | &item.vis, | |
1001 | Some("place qualifiers on individual impl items instead"), | |
1002 | ); | |
74b04a01 | 1003 | if let Unsafe::Yes(span) = unsafety { |
ba9703b0 | 1004 | error(span, "unsafe").code(error_code!(E0197)).emit(); |
2c00a5a8 | 1005 | } |
ba9703b0 XL |
1006 | if let ImplPolarity::Negative(span) = polarity { |
1007 | error(span, "negative").emit(); | |
2c00a5a8 | 1008 | } |
74b04a01 | 1009 | if let Defaultness::Default(def_span) = defaultness { |
ba9703b0 | 1010 | error(def_span, "`default`") |
74b04a01 | 1011 | .note("only trait implementations may be annotated with `default`") |
dfeec247 XL |
1012 | .emit(); |
1013 | } | |
74b04a01 | 1014 | if let Const::Yes(span) = constness { |
ba9703b0 | 1015 | error(span, "`const`") |
dfeec247 XL |
1016 | .note("only trait implementations may be annotated with `const`") |
1017 | .emit(); | |
2c00a5a8 | 1018 | } |
3157f602 | 1019 | } |
5869c6ff | 1020 | ItemKind::Fn(box FnKind(def, _, _, ref body)) => { |
74b04a01 | 1021 | self.check_defaultness(item.span, def); |
74b04a01 XL |
1022 | |
1023 | if body.is_none() { | |
1024 | let msg = "free function without a body"; | |
1025 | self.error_item_without_body(item.span, "function", msg, " { <body> }"); | |
dfeec247 | 1026 | } |
9fa01778 | 1027 | } |
1b1a35ee | 1028 | ItemKind::ForeignMod(ForeignMod { unsafety, .. }) => { |
74b04a01 | 1029 | let old_item = mem::replace(&mut self.extern_mod, Some(item)); |
0531ce1d XL |
1030 | self.invalid_visibility( |
1031 | &item.vis, | |
1032 | Some("place qualifiers on individual foreign items instead"), | |
1033 | ); | |
1b1a35ee XL |
1034 | if let Unsafe::Yes(span) = unsafety { |
1035 | self.err_handler().span_err(span, "extern block cannot be declared unsafe"); | |
1036 | } | |
74b04a01 XL |
1037 | visit::walk_item(self, item); |
1038 | self.extern_mod = old_item; | |
1039 | return; // Avoid visiting again. | |
3157f602 XL |
1040 | } |
1041 | ItemKind::Enum(ref def, _) => { | |
1042 | for variant in &def.variants { | |
60c5eb7d | 1043 | self.invalid_visibility(&variant.vis, None); |
e1599b0c | 1044 | for field in variant.data.fields() { |
0531ce1d | 1045 | self.invalid_visibility(&field.vis, None); |
3157f602 XL |
1046 | } |
1047 | } | |
1048 | } | |
5869c6ff XL |
1049 | ItemKind::Trait(box TraitKind( |
1050 | is_auto, | |
1051 | _, | |
1052 | ref generics, | |
1053 | ref bounds, | |
1054 | ref trait_items, | |
1055 | )) => { | |
abe05a73 XL |
1056 | if is_auto == IsAuto::Yes { |
1057 | // Auto traits cannot have generics, super traits nor contain items. | |
ba9703b0 XL |
1058 | self.deny_generic_params(generics, item.ident.span); |
1059 | self.deny_super_traits(bounds, item.ident.span); | |
1060 | self.deny_items(trait_items, item.ident.span); | |
abe05a73 | 1061 | } |
476ff2be | 1062 | self.no_questions_in_bounds(bounds, "supertraits", true); |
dfeec247 XL |
1063 | |
1064 | // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound | |
1065 | // context for the supertraits. | |
1066 | self.visit_vis(&item.vis); | |
1067 | self.visit_ident(item.ident); | |
1068 | self.visit_generics(generics); | |
1069 | self.with_bound_context(BoundContext::TraitBounds, |this| { | |
1070 | walk_list!(this, visit_param_bound, bounds); | |
1071 | }); | |
74b04a01 | 1072 | walk_list!(self, visit_assoc_item, trait_items, AssocCtxt::Trait); |
dfeec247 XL |
1073 | walk_list!(self, visit_attribute, &item.attrs); |
1074 | return; | |
9e0c209e | 1075 | } |
6a06907d | 1076 | ItemKind::Mod(unsafety, ref mod_kind) => { |
1b1a35ee XL |
1077 | if let Unsafe::Yes(span) = unsafety { |
1078 | self.err_handler().span_err(span, "module cannot be declared unsafe"); | |
1079 | } | |
0731742a | 1080 | // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584). |
6a06907d XL |
1081 | if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) |
1082 | && !self.session.contains_name(&item.attrs, sym::path) | |
1083 | { | |
f9f354fc XL |
1084 | self.check_mod_file_item_asciionly(item.ident); |
1085 | } | |
5bcae85e | 1086 | } |
9e0c209e | 1087 | ItemKind::Union(ref vdata, _) => { |
532ac7d7 | 1088 | if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata { |
dfeec247 XL |
1089 | self.err_handler() |
1090 | .span_err(item.span, "tuple and unit unions are not permitted"); | |
9e0c209e | 1091 | } |
b7449926 | 1092 | if vdata.fields().is_empty() { |
dfeec247 | 1093 | self.err_handler().span_err(item.span, "unions cannot have zero fields"); |
9e0c209e SL |
1094 | } |
1095 | } | |
74b04a01 XL |
1096 | ItemKind::Const(def, .., None) => { |
1097 | self.check_defaultness(item.span, def); | |
1098 | let msg = "free constant item without body"; | |
1099 | self.error_item_without_body(item.span, "constant", msg, " = <expr>;"); | |
1100 | } | |
1101 | ItemKind::Static(.., None) => { | |
1102 | let msg = "free static item without body"; | |
1103 | self.error_item_without_body(item.span, "static", msg, " = <expr>;"); | |
1104 | } | |
5869c6ff | 1105 | ItemKind::TyAlias(box TyAliasKind(def, _, ref bounds, ref body)) => { |
74b04a01 XL |
1106 | self.check_defaultness(item.span, def); |
1107 | if body.is_none() { | |
1108 | let msg = "free type alias without body"; | |
1109 | self.error_item_without_body(item.span, "type", msg, " = <type>;"); | |
1110 | } | |
1111 | self.check_type_no_bounds(bounds, "this context"); | |
1112 | } | |
3157f602 XL |
1113 | _ => {} |
1114 | } | |
1115 | ||
1116 | visit::walk_item(self, item) | |
1117 | } | |
1118 | ||
476ff2be | 1119 | fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { |
74b04a01 | 1120 | match &fi.kind { |
5869c6ff | 1121 | ForeignItemKind::Fn(box FnKind(def, sig, _, body)) => { |
74b04a01 XL |
1122 | self.check_defaultness(fi.span, *def); |
1123 | self.check_foreign_fn_bodyless(fi.ident, body.as_deref()); | |
1124 | self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header); | |
cdc7bbd5 | 1125 | self.check_foreign_item_ascii_only(fi.ident); |
3157f602 | 1126 | } |
5869c6ff | 1127 | ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => { |
74b04a01 XL |
1128 | self.check_defaultness(fi.span, *def); |
1129 | self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span)); | |
1130 | self.check_type_no_bounds(bounds, "`extern` blocks"); | |
1131 | self.check_foreign_ty_genericless(generics); | |
cdc7bbd5 | 1132 | self.check_foreign_item_ascii_only(fi.ident); |
74b04a01 XL |
1133 | } |
1134 | ForeignItemKind::Static(_, _, body) => { | |
1135 | self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span)); | |
cdc7bbd5 | 1136 | self.check_foreign_item_ascii_only(fi.ident); |
74b04a01 | 1137 | } |
ba9703b0 | 1138 | ForeignItemKind::MacCall(..) => {} |
3157f602 XL |
1139 | } |
1140 | ||
5bcae85e | 1141 | visit::walk_foreign_item(self, fi) |
3157f602 XL |
1142 | } |
1143 | ||
dc9dc135 | 1144 | // Mirrors `visit::walk_generic_args`, but tracks relevant state. |
9fa01778 XL |
1145 | fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) { |
1146 | match *generic_args { | |
1147 | GenericArgs::AngleBracketed(ref data) => { | |
ba9703b0 XL |
1148 | self.check_generic_args_before_constraints(data); |
1149 | ||
1150 | for arg in &data.args { | |
1151 | match arg { | |
1152 | AngleBracketedArg::Arg(arg) => self.visit_generic_arg(arg), | |
1153 | // Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>` | |
1154 | // are allowed to contain nested `impl Trait`. | |
1155 | AngleBracketedArg::Constraint(constraint) => { | |
1156 | self.with_impl_trait(None, |this| { | |
1157 | this.visit_assoc_ty_constraint_from_generic_args(constraint); | |
1158 | }); | |
1159 | } | |
1160 | } | |
1161 | } | |
9fa01778 XL |
1162 | } |
1163 | GenericArgs::Parenthesized(ref data) => { | |
1164 | walk_list!(self, visit_ty, &data.inputs); | |
74b04a01 | 1165 | if let FnRetTy::Ty(ty) = &data.output { |
9fa01778 XL |
1166 | // `-> Foo` syntax is essentially an associated type binding, |
1167 | // so it is also allowed to contain nested `impl Trait`. | |
dfeec247 | 1168 | self.with_impl_trait(None, |this| this.visit_ty(ty)); |
9fa01778 XL |
1169 | } |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | ||
8faf50e0 | 1174 | fn visit_generics(&mut self, generics: &'a Generics) { |
cdc7bbd5 XL |
1175 | let cg_defaults = self.session.features_untracked().const_generics_defaults; |
1176 | ||
1177 | let mut prev_param_default = None; | |
8faf50e0 | 1178 | for param in &generics.params { |
3dfed10e XL |
1179 | match param.kind { |
1180 | GenericParamKind::Lifetime => (), | |
cdc7bbd5 XL |
1181 | GenericParamKind::Type { default: Some(_), .. } |
1182 | | GenericParamKind::Const { default: Some(_), .. } => { | |
1183 | prev_param_default = Some(param.ident.span); | |
3dfed10e XL |
1184 | } |
1185 | GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { | |
cdc7bbd5 | 1186 | if let Some(span) = prev_param_default { |
3dfed10e XL |
1187 | let mut err = self.err_handler().struct_span_err( |
1188 | span, | |
cdc7bbd5 | 1189 | "generic parameters with a default must be trailing", |
3dfed10e | 1190 | ); |
cdc7bbd5 | 1191 | if matches!(param.kind, GenericParamKind::Const { .. }) && !cg_defaults { |
3dfed10e XL |
1192 | err.note( |
1193 | "using type defaults and const parameters \ | |
1194 | in the same parameter list is currently not permitted", | |
1195 | ); | |
1196 | } | |
1197 | err.emit(); | |
1198 | break; | |
1199 | } | |
ff7c6d11 XL |
1200 | } |
1201 | } | |
32a655c1 | 1202 | } |
9fa01778 | 1203 | |
74b04a01 | 1204 | validate_generic_param_order( |
532ac7d7 XL |
1205 | self.session, |
1206 | self.err_handler(), | |
5869c6ff | 1207 | &generics.params, |
532ac7d7 XL |
1208 | generics.span, |
1209 | ); | |
9fa01778 | 1210 | |
8faf50e0 | 1211 | for predicate in &generics.where_clause.predicates { |
32a655c1 | 1212 | if let WherePredicate::EqPredicate(ref predicate) = *predicate { |
f9f354fc | 1213 | deny_equality_constraints(self, predicate, generics); |
32a655c1 SL |
1214 | } |
1215 | } | |
cdc7bbd5 XL |
1216 | walk_list!(self, visit_generic_param, &generics.params); |
1217 | for predicate in &generics.where_clause.predicates { | |
1218 | match predicate { | |
1219 | WherePredicate::BoundPredicate(bound_pred) => { | |
1220 | // A type binding, eg `for<'c> Foo: Send+Clone+'c` | |
1221 | self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params); | |
1222 | ||
1223 | // This is slightly complicated. Our representation for poly-trait-refs contains a single | |
1224 | // binder and thus we only allow a single level of quantification. However, | |
1225 | // the syntax of Rust permits quantification in two places in where clauses, | |
1226 | // e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are | |
1227 | // defined, then error. | |
1228 | if !bound_pred.bound_generic_params.is_empty() { | |
1229 | for bound in &bound_pred.bounds { | |
1230 | match bound { | |
1231 | GenericBound::Trait(t, _) => { | |
1232 | if !t.bound_generic_params.is_empty() { | |
1233 | struct_span_err!( | |
1234 | self.err_handler(), | |
1235 | t.span, | |
1236 | E0316, | |
1237 | "nested quantification of lifetimes" | |
1238 | ) | |
1239 | .emit(); | |
1240 | } | |
1241 | } | |
1242 | GenericBound::Outlives(_) => {} | |
1243 | } | |
1244 | } | |
1245 | } | |
1246 | } | |
1247 | _ => {} | |
1248 | } | |
1249 | self.visit_where_predicate(predicate); | |
1250 | } | |
32a655c1 | 1251 | } |
041b39d2 | 1252 | |
94b46f34 | 1253 | fn visit_generic_param(&mut self, param: &'a GenericParam) { |
8faf50e0 XL |
1254 | if let GenericParamKind::Lifetime { .. } = param.kind { |
1255 | self.check_lifetime(param.ident); | |
94b46f34 XL |
1256 | } |
1257 | visit::walk_generic_param(self, param); | |
1258 | } | |
1259 | ||
dfeec247 XL |
1260 | fn visit_param_bound(&mut self, bound: &'a GenericBound) { |
1261 | match bound { | |
1262 | GenericBound::Trait(_, TraitBoundModifier::MaybeConst) => { | |
1263 | if let Some(ctx) = self.bound_context { | |
1264 | let msg = format!("`?const` is not permitted in {}", ctx.description()); | |
1265 | self.err_handler().span_err(bound.span(), &msg); | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | GenericBound::Trait(_, TraitBoundModifier::MaybeConstMaybe) => { | |
1270 | self.err_handler() | |
1271 | .span_err(bound.span(), "`?const` and `?` are mutually exclusive"); | |
1272 | } | |
1273 | ||
1274 | _ => {} | |
1275 | } | |
1276 | ||
1277 | visit::walk_param_bound(self, bound) | |
1278 | } | |
1279 | ||
041b39d2 | 1280 | fn visit_pat(&mut self, pat: &'a Pat) { |
5869c6ff XL |
1281 | match &pat.kind { |
1282 | PatKind::Lit(expr) => { | |
041b39d2 XL |
1283 | self.check_expr_within_pat(expr, false); |
1284 | } | |
5869c6ff | 1285 | PatKind::Range(start, end, _) => { |
dfeec247 XL |
1286 | if let Some(expr) = start { |
1287 | self.check_expr_within_pat(expr, true); | |
1288 | } | |
1289 | if let Some(expr) = end { | |
1290 | self.check_expr_within_pat(expr, true); | |
1291 | } | |
041b39d2 XL |
1292 | } |
1293 | _ => {} | |
1294 | } | |
1295 | ||
1296 | visit::walk_pat(self, pat) | |
1297 | } | |
0531ce1d | 1298 | |
0531ce1d XL |
1299 | fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) { |
1300 | self.check_late_bound_lifetime_defs(&t.bound_generic_params); | |
1301 | visit::walk_poly_trait_ref(self, t, m); | |
1302 | } | |
83c7162d | 1303 | |
e1599b0c | 1304 | fn visit_variant_data(&mut self, s: &'a VariantData) { |
dc9dc135 XL |
1305 | self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s)) |
1306 | } | |
1307 | ||
dfeec247 XL |
1308 | fn visit_enum_def( |
1309 | &mut self, | |
1310 | enum_definition: &'a EnumDef, | |
1311 | generics: &'a Generics, | |
1312 | item_id: NodeId, | |
1313 | _: Span, | |
1314 | ) { | |
1315 | self.with_banned_assoc_ty_bound(|this| { | |
1316 | visit::walk_enum_def(this, enum_definition, generics, item_id) | |
1317 | }) | |
dc9dc135 XL |
1318 | } |
1319 | ||
74b04a01 XL |
1320 | fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) { |
1321 | // Only associated `fn`s can have `self` parameters. | |
1322 | let self_semantic = match fk.ctxt() { | |
1323 | Some(FnCtxt::Assoc(_)) => SelfSemantic::Yes, | |
1324 | _ => SelfSemantic::No, | |
1325 | }; | |
1326 | self.check_fn_decl(fk.decl(), self_semantic); | |
1327 | ||
1328 | self.check_c_varadic_type(fk); | |
1329 | ||
1330 | // Functions cannot both be `const async` | |
1331 | if let Some(FnHeader { | |
1332 | constness: Const::Yes(cspan), | |
1333 | asyncness: Async::Yes { span: aspan, .. }, | |
1334 | .. | |
1335 | }) = fk.header() | |
1336 | { | |
1337 | self.err_handler() | |
ba9703b0 XL |
1338 | .struct_span_err( |
1339 | vec![*cspan, *aspan], | |
1340 | "functions cannot be both `const` and `async`", | |
1341 | ) | |
74b04a01 XL |
1342 | .span_label(*cspan, "`const` because of this") |
1343 | .span_label(*aspan, "`async` because of this") | |
ba9703b0 | 1344 | .span_label(span, "") // Point at the fn header. |
74b04a01 | 1345 | .emit(); |
532ac7d7 | 1346 | } |
60c5eb7d | 1347 | |
74b04a01 XL |
1348 | // Functions without bodies cannot have patterns. |
1349 | if let FnKind::Fn(ctxt, _, sig, _, None) = fk { | |
5869c6ff | 1350 | Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| { |
74b04a01 XL |
1351 | let (code, msg, label) = match ctxt { |
1352 | FnCtxt::Foreign => ( | |
1353 | error_code!(E0130), | |
1354 | "patterns aren't allowed in foreign function declarations", | |
1355 | "pattern not allowed in foreign function", | |
1356 | ), | |
1357 | _ => ( | |
1358 | error_code!(E0642), | |
1359 | "patterns aren't allowed in functions without bodies", | |
1360 | "pattern not allowed in function without body", | |
1361 | ), | |
1362 | }; | |
1363 | if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) { | |
5869c6ff XL |
1364 | if let Some(ident) = ident { |
1365 | let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident); | |
1366 | self.lint_buffer.buffer_lint_with_diagnostic( | |
1367 | PATTERNS_IN_FNS_WITHOUT_BODY, | |
1368 | id, | |
1369 | span, | |
1370 | msg, | |
1371 | diag, | |
1372 | ) | |
1373 | } | |
74b04a01 XL |
1374 | } else { |
1375 | self.err_handler() | |
1376 | .struct_span_err(span, msg) | |
1377 | .span_label(span, label) | |
1378 | .code(code) | |
dfeec247 | 1379 | .emit(); |
74b04a01 XL |
1380 | } |
1381 | }); | |
dfeec247 XL |
1382 | } |
1383 | ||
74b04a01 | 1384 | visit::walk_fn(self, fk, span); |
60c5eb7d | 1385 | } |
dfeec247 | 1386 | |
74b04a01 XL |
1387 | fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) { |
1388 | if ctxt == AssocCtxt::Trait || !self.in_trait_impl { | |
1389 | self.check_defaultness(item.span, item.kind.defaultness()); | |
1390 | } | |
1391 | ||
1392 | if ctxt == AssocCtxt::Impl { | |
1393 | match &item.kind { | |
1394 | AssocItemKind::Const(_, _, body) => { | |
1395 | self.check_impl_item_provided(item.span, body, "constant", " = <expr>;"); | |
1396 | } | |
5869c6ff | 1397 | AssocItemKind::Fn(box FnKind(_, _, _, body)) => { |
74b04a01 XL |
1398 | self.check_impl_item_provided(item.span, body, "function", " { <body> }"); |
1399 | } | |
5869c6ff | 1400 | AssocItemKind::TyAlias(box TyAliasKind(_, _, bounds, body)) => { |
74b04a01 XL |
1401 | self.check_impl_item_provided(item.span, body, "type", " = <type>;"); |
1402 | self.check_type_no_bounds(bounds, "`impl`s"); | |
1403 | } | |
1404 | _ => {} | |
1405 | } | |
dfeec247 | 1406 | } |
74b04a01 XL |
1407 | |
1408 | if ctxt == AssocCtxt::Trait || self.in_trait_impl { | |
1409 | self.invalid_visibility(&item.vis, None); | |
5869c6ff | 1410 | if let AssocItemKind::Fn(box FnKind(_, sig, _, _)) = &item.kind { |
74b04a01 XL |
1411 | self.check_trait_fn_not_const(sig.header.constness); |
1412 | self.check_trait_fn_not_async(item.span, sig.header.asyncness); | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | if let AssocItemKind::Const(..) = item.kind { | |
1417 | self.check_item_named(item.ident, "const"); | |
1418 | } | |
1419 | ||
1420 | self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt)); | |
dfeec247 | 1421 | } |
0531ce1d XL |
1422 | } |
1423 | ||
f9f354fc XL |
1424 | /// When encountering an equality constraint in a `where` clause, emit an error. If the code seems |
1425 | /// like it's setting an associated type, provide an appropriate suggestion. | |
1426 | fn deny_equality_constraints( | |
1427 | this: &mut AstValidator<'_>, | |
1428 | predicate: &WhereEqPredicate, | |
1429 | generics: &Generics, | |
1430 | ) { | |
1431 | let mut err = this.err_handler().struct_span_err( | |
1432 | predicate.span, | |
1433 | "equality constraints are not yet supported in `where` clauses", | |
1434 | ); | |
1435 | err.span_label(predicate.span, "not supported"); | |
1436 | ||
1437 | // Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`. | |
1438 | if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind { | |
1439 | if let TyKind::Path(None, path) = &qself.ty.kind { | |
1440 | match &path.segments[..] { | |
1441 | [PathSegment { ident, args: None, .. }] => { | |
1442 | for param in &generics.params { | |
1443 | if param.ident == *ident { | |
1444 | let param = ident; | |
1445 | match &full_path.segments[qself.position..] { | |
fc512014 | 1446 | [PathSegment { ident, args, .. }] => { |
f9f354fc XL |
1447 | // Make a new `Path` from `foo::Bar` to `Foo<Bar = RhsTy>`. |
1448 | let mut assoc_path = full_path.clone(); | |
1449 | // Remove `Bar` from `Foo::Bar`. | |
1450 | assoc_path.segments.pop(); | |
1451 | let len = assoc_path.segments.len() - 1; | |
fc512014 | 1452 | let gen_args = args.as_ref().map(|p| (**p).clone()); |
f9f354fc XL |
1453 | // Build `<Bar = RhsTy>`. |
1454 | let arg = AngleBracketedArg::Constraint(AssocTyConstraint { | |
1455 | id: rustc_ast::node_id::DUMMY_NODE_ID, | |
1456 | ident: *ident, | |
fc512014 | 1457 | gen_args, |
f9f354fc XL |
1458 | kind: AssocTyConstraintKind::Equality { |
1459 | ty: predicate.rhs_ty.clone(), | |
1460 | }, | |
1461 | span: ident.span, | |
1462 | }); | |
1463 | // Add `<Bar = RhsTy>` to `Foo`. | |
1464 | match &mut assoc_path.segments[len].args { | |
1465 | Some(args) => match args.deref_mut() { | |
1466 | GenericArgs::Parenthesized(_) => continue, | |
1467 | GenericArgs::AngleBracketed(args) => { | |
1468 | args.args.push(arg); | |
1469 | } | |
1470 | }, | |
1471 | empty_args => { | |
1472 | *empty_args = AngleBracketedArgs { | |
1473 | span: ident.span, | |
1474 | args: vec![arg], | |
1475 | } | |
1476 | .into(); | |
1477 | } | |
1478 | } | |
1479 | err.span_suggestion_verbose( | |
1480 | predicate.span, | |
1481 | &format!( | |
1482 | "if `{}` is an associated type you're trying to set, \ | |
1483 | use the associated type binding syntax", | |
1484 | ident | |
1485 | ), | |
1486 | format!( | |
1487 | "{}: {}", | |
1488 | param, | |
1489 | pprust::path_to_string(&assoc_path) | |
1490 | ), | |
1491 | Applicability::MaybeIncorrect, | |
1492 | ); | |
1493 | } | |
1494 | _ => {} | |
1495 | }; | |
1496 | } | |
1497 | } | |
1498 | } | |
1499 | _ => {} | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | err.note( | |
1504 | "see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information", | |
1505 | ); | |
1506 | err.emit(); | |
1507 | } | |
1508 | ||
dfeec247 | 1509 | pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool { |
9fa01778 XL |
1510 | let mut validator = AstValidator { |
1511 | session, | |
74b04a01 XL |
1512 | extern_mod: None, |
1513 | in_trait_impl: false, | |
9fa01778 | 1514 | has_proc_macro_decls: false, |
9fa01778 | 1515 | outer_impl_trait: None, |
dfeec247 | 1516 | bound_context: None, |
9fa01778 | 1517 | is_impl_trait_banned: false, |
dc9dc135 | 1518 | is_assoc_ty_bound_banned: false, |
e74abb32 | 1519 | lint_buffer: lints, |
9fa01778 XL |
1520 | }; |
1521 | visit::walk_crate(&mut validator, krate); | |
1522 | ||
416331ca | 1523 | validator.has_proc_macro_decls |
3157f602 | 1524 | } |