1 // Validate AST before lowering it to HIR.
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.
11 use rustc::session::Session;
12 use rustc_data_structures::fx::FxHashMap;
13 use rustc_parse::validate_attr;
16 use syntax::expand::is_proc_macro_attr;
17 use syntax::print::pprust;
18 use syntax::source_map::Spanned;
19 use syntax::symbol::{kw, sym};
20 use syntax::visit::{self, Visitor};
21 use syntax::{span_err, struct_span_err, walk_list};
23 use errors::{Applicability, FatalError};
25 use rustc_error_codes::*;
27 struct AstValidator<'a> {
29 has_proc_macro_decls: bool,
31 /// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
32 /// Nested `impl Trait` _is_ allowed in associated type position,
33 /// e.g., `impl Iterator<Item = impl Debug>`.
34 outer_impl_trait: Option<Span>,
36 /// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
37 /// or `Foo::Bar<impl Trait>`
38 is_impl_trait_banned: bool,
40 /// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
41 /// certain positions.
42 is_assoc_ty_bound_banned: bool,
44 lint_buffer: &'a mut lint::LintBuffer,
47 impl<'a> AstValidator<'a> {
48 fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
49 let old = mem::replace(&mut self.is_impl_trait_banned, true);
51 self.is_impl_trait_banned = old;
54 fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
55 let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true);
57 self.is_assoc_ty_bound_banned = old;
60 fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
61 let old = mem::replace(&mut self.outer_impl_trait, outer);
63 self.outer_impl_trait = old;
66 fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) {
67 match constraint.kind {
68 AssocTyConstraintKind::Equality { .. } => {}
69 AssocTyConstraintKind::Bound { .. } => {
70 if self.is_assoc_ty_bound_banned {
71 self.err_handler().span_err(constraint.span,
72 "associated type bounds are not allowed within structs, enums, or unions"
77 self.visit_assoc_ty_constraint(constraint);
80 // Mirrors `visit::walk_ty`, but tracks relevant state.
81 fn walk_ty(&mut self, t: &'a Ty) {
83 TyKind::ImplTrait(..) => {
84 self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
86 TyKind::Path(ref qself, ref path) => {
88 // - `Option<impl Trait>`
89 // - `option::Option<impl Trait>`
90 // - `option::Option<T>::Foo<impl Trait>
93 // - `<impl Trait>::Foo`
94 // - `option::Option<impl Trait>::Foo`.
96 // To implement this, we disallow `impl Trait` from `qself`
97 // (for cases like `<impl Trait>::Foo>`)
98 // but we allow `impl Trait` in `GenericArgs`
99 // iff there are no more PathSegments.
100 if let Some(ref qself) = *qself {
101 // `impl Trait` in `qself` is always illegal
102 self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty));
105 // Note that there should be a call to visit_path here,
106 // so if any logic is added to process `Path`s a call to it should be
107 // added both in visit_path and here. This code mirrors visit::walk_path.
108 for (i, segment) in path.segments.iter().enumerate() {
109 // Allow `impl Trait` iff we're on the final path segment
110 if i == path.segments.len() - 1 {
111 self.visit_path_segment(path.span, segment);
113 self.with_banned_impl_trait(|this| {
114 this.visit_path_segment(path.span, segment)
119 _ => visit::walk_ty(self, t),
123 fn err_handler(&self) -> &errors::Handler {
124 &self.session.diagnostic()
127 fn check_lifetime(&self, ident: Ident) {
128 let valid_names = [kw::UnderscoreLifetime,
131 if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
132 self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
136 fn check_label(&self, ident: Ident) {
137 if ident.without_first_quote().is_reserved() {
139 .span_err(ident.span, &format!("invalid label name `{}`", ident.name));
143 fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
144 if let VisibilityKind::Inherited = vis.node {
148 let mut err = struct_span_err!(self.session,
151 "unnecessary visibility qualifier");
152 if vis.node.is_pub() {
153 err.span_label(vis.span, "`pub` not permitted here because it's implied");
155 if let Some(note) = note {
161 fn check_decl_no_pat<F: FnMut(Span, bool)>(decl: &FnDecl, mut report_err: F) {
162 for arg in &decl.inputs {
164 PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
166 PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), _, None) =>
167 report_err(arg.pat.span, true),
168 _ => report_err(arg.pat.span, false),
173 fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
174 if asyncness.is_async() {
175 struct_span_err!(self.session, span, E0706, "trait fns cannot be declared `async`")
176 .note("`async` trait functions are not currently supported")
177 .note("consider using the `async-trait` crate: \
178 https://crates.io/crates/async-trait")
183 fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
184 if constness.node == Constness::Const {
185 struct_span_err!(self.session, constness.span, E0379,
186 "trait fns cannot be declared const")
187 .span_label(constness.span, "trait fns cannot be const")
192 fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
193 for bound in bounds {
194 if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
195 let mut err = self.err_handler().struct_span_err(poly.span,
196 &format!("`?Trait` is not permitted in {}", where_));
198 let path_str = pprust::path_to_string(&poly.trait_ref.path);
199 err.note(&format!("traits are `?{}` by default", path_str));
206 /// Matches `'-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus)`,
207 /// or paths for ranges.
209 // FIXME: do we want to allow `expr -> pattern` conversion to create path expressions?
210 // That means making this work:
212 // ```rust,ignore (FIXME)
221 fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
223 ExprKind::Lit(..) | ExprKind::Err => {}
224 ExprKind::Path(..) if allow_paths => {}
225 ExprKind::Unary(UnOp::Neg, ref inner)
226 if match inner.kind { ExprKind::Lit(_) => true, _ => false } => {}
227 _ => self.err_handler().span_err(expr.span, "arbitrary expressions aren't allowed \
232 fn check_late_bound_lifetime_defs(&self, params: &[GenericParam]) {
233 // Check only lifetime parameters are present and that the lifetime
234 // parameters that are present have no bounds.
235 let non_lt_param_spans: Vec<_> = params.iter().filter_map(|param| match param.kind {
236 GenericParamKind::Lifetime { .. } => {
237 if !param.bounds.is_empty() {
238 let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
240 .span_err(spans, "lifetime bounds cannot be used in this context");
244 _ => Some(param.ident.span),
246 if !non_lt_param_spans.is_empty() {
247 self.err_handler().span_err(non_lt_param_spans,
248 "only lifetime parameters can be used in this context");
252 fn check_fn_decl(&self, fn_decl: &FnDecl) {
256 .flat_map(|i| i.attrs.as_ref())
258 let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn];
259 !arr.contains(&attr.name_or_empty()) && attr::is_builtin_attr(attr)
261 .for_each(|attr| if attr.is_doc_comment() {
262 let mut err = self.err_handler().struct_span_err(
264 "documentation comments cannot be applied to function parameters"
266 err.span_label(attr.span, "doc comments are not allowed here");
270 self.err_handler().span_err(attr.span, "allow, cfg, cfg_attr, deny, \
271 forbid, and warn are the only allowed built-in attributes in function parameters")
276 enum GenericPosition {
281 fn validate_generics_order<'a>(
283 handler: &errors::Handler,
284 generics: impl Iterator<
287 Option<&'a [GenericBound]>,
292 pos: GenericPosition,
295 let mut max_param: Option<ParamKindOrd> = None;
296 let mut out_of_order = FxHashMap::default();
297 let mut param_idents = vec![];
298 let mut found_type = false;
299 let mut found_const = false;
301 for (kind, bounds, span, ident) in generics {
302 if let Some(ident) = ident {
303 param_idents.push((kind, bounds, param_idents.len(), ident));
305 let max_param = &mut max_param;
307 Some(max_param) if *max_param > kind => {
308 let entry = out_of_order.entry(kind).or_insert((*max_param, vec![]));
311 Some(_) | None => *max_param = Some(kind),
314 ParamKindOrd::Type => found_type = true,
315 ParamKindOrd::Const => found_const = true,
320 let mut ordered_params = "<".to_string();
321 if !out_of_order.is_empty() {
322 param_idents.sort_by_key(|&(po, _, i, _)| (po, i));
323 let mut first = true;
324 for (_, bounds, _, ident) in param_idents {
326 ordered_params += ", ";
328 ordered_params += &ident;
329 if let Some(bounds) = bounds {
330 if !bounds.is_empty() {
331 ordered_params += ": ";
332 ordered_params += &pprust::bounds_to_string(&bounds);
338 ordered_params += ">";
340 let pos_str = match pos {
341 GenericPosition::Param => "parameter",
342 GenericPosition::Arg => "argument",
345 for (param_ord, (max_param, spans)) in &out_of_order {
346 let mut err = handler.struct_span_err(spans.clone(),
348 "{} {pos}s must be declared prior to {} {pos}s",
353 if let GenericPosition::Param = pos {
357 "reorder the {}s: lifetimes, then types{}",
359 if sess.features_untracked().const_generics { ", then consts" } else { "" },
361 ordered_params.clone(),
362 Applicability::MachineApplicable,
368 // FIXME(const_generics): we shouldn't have to abort here at all, but we currently get ICEs
369 // if we don't. Const parameters and type parameters can currently conflict if they
371 if !out_of_order.is_empty() && found_type && found_const {
376 impl<'a> Visitor<'a> for AstValidator<'a> {
377 fn visit_attribute(&mut self, attr: &Attribute) {
378 validate_attr::check_meta(&self.session.parse_sess, attr);
381 fn visit_expr(&mut self, expr: &'a Expr) {
383 ExprKind::Closure(_, _, _, fn_decl, _, _) => {
384 self.check_fn_decl(fn_decl);
386 ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
387 span_err!(self.session, expr.span, E0472, "asm! is unsupported on this target");
392 visit::walk_expr(self, expr);
395 fn visit_ty(&mut self, ty: &'a Ty) {
397 TyKind::BareFn(ref bfty) => {
398 self.check_fn_decl(&bfty.decl);
399 Self::check_decl_no_pat(&bfty.decl, |span, _| {
400 struct_span_err!(self.session, span, E0561,
401 "patterns aren't allowed in function pointer types").emit();
403 self.check_late_bound_lifetime_defs(&bfty.generic_params);
405 TyKind::TraitObject(ref bounds, ..) => {
406 let mut any_lifetime_bounds = false;
407 for bound in bounds {
408 if let GenericBound::Outlives(ref lifetime) = *bound {
409 if any_lifetime_bounds {
410 span_err!(self.session, lifetime.ident.span, E0226,
411 "only a single explicit lifetime bound is permitted");
414 any_lifetime_bounds = true;
417 self.no_questions_in_bounds(bounds, "trait object types", false);
419 TyKind::ImplTrait(_, ref bounds) => {
420 if self.is_impl_trait_banned {
422 self.session, ty.span, E0667,
423 "`impl Trait` is not allowed in path parameters"
428 if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
430 self.session, ty.span, E0666,
431 "nested `impl Trait` is not allowed"
433 .span_label(outer_impl_trait_sp, "outer `impl Trait`")
434 .span_label(ty.span, "nested `impl Trait` here")
439 .any(|b| if let GenericBound::Trait(..) = *b { true } else { false }) {
440 self.err_handler().span_err(ty.span, "at least one trait must be specified");
452 fn visit_label(&mut self, label: &'a Label) {
453 self.check_label(label.ident);
454 visit::walk_label(self, label);
457 fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
458 self.check_lifetime(lifetime.ident);
459 visit::walk_lifetime(self, lifetime);
462 fn visit_item(&mut self, item: &'a Item) {
463 if item.attrs.iter().any(|attr| is_proc_macro_attr(attr) ) {
464 self.has_proc_macro_decls = true;
468 ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
469 self.invalid_visibility(&item.vis, None);
470 if let TyKind::Err = ty.kind {
472 .struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
473 .help("use `auto trait Trait {}` instead").emit();
475 if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
476 span_err!(self.session, item.span, E0198, "negative impls cannot be unsafe");
478 for impl_item in impl_items {
479 self.invalid_visibility(&impl_item.vis, None);
480 if let ImplItemKind::Method(ref sig, _) = impl_item.kind {
481 self.check_trait_fn_not_const(sig.header.constness);
482 self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
486 ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
487 self.invalid_visibility(&item.vis,
488 Some("place qualifiers on individual impl items instead"));
489 if unsafety == Unsafety::Unsafe {
490 span_err!(self.session, item.span, E0197, "inherent impls cannot be unsafe");
492 if polarity == ImplPolarity::Negative {
493 self.err_handler().span_err(item.span, "inherent impls cannot be negative");
495 if defaultness == Defaultness::Default {
497 .struct_span_err(item.span, "inherent impls cannot be default")
498 .note("only trait implementations may be annotated with default").emit();
501 ItemKind::Fn(ref sig, ref generics, _) => {
502 self.visit_fn_header(&sig.header);
503 self.check_fn_decl(&sig.decl);
504 // We currently do not permit const generics in `const fn`, as
505 // this is tantamount to allowing compile-time dependent typing.
506 if sig.header.constness.node == Constness::Const {
507 // Look for const generics and error if we find any.
508 for param in &generics.params {
510 GenericParamKind::Const { .. } => {
514 "const parameters are not permitted in `const fn`",
523 ItemKind::ForeignMod(..) => {
524 self.invalid_visibility(
526 Some("place qualifiers on individual foreign items instead"),
529 ItemKind::Enum(ref def, _) => {
530 for variant in &def.variants {
531 self.invalid_visibility(&variant.vis, None);
532 for field in variant.data.fields() {
533 self.invalid_visibility(&field.vis, None);
537 ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
538 if is_auto == IsAuto::Yes {
539 // Auto traits cannot have generics, super traits nor contain items.
540 if !generics.params.is_empty() {
541 struct_span_err!(self.session, item.span, E0567,
542 "auto traits cannot have generic parameters"
545 if !bounds.is_empty() {
546 struct_span_err!(self.session, item.span, E0568,
547 "auto traits cannot have super traits"
550 if !trait_items.is_empty() {
551 struct_span_err!(self.session, item.span, E0380,
552 "auto traits cannot have methods or associated items"
556 self.no_questions_in_bounds(bounds, "supertraits", true);
557 for trait_item in trait_items {
558 if let TraitItemKind::Method(ref sig, ref block) = trait_item.kind {
559 self.check_fn_decl(&sig.decl);
560 self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness.node);
561 self.check_trait_fn_not_const(sig.header.constness);
563 Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
565 self.lint_buffer.buffer_lint(
566 lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
568 "patterns aren't allowed in methods without bodies");
570 struct_span_err!(self.session, span, E0642,
571 "patterns aren't allowed in methods without bodies").emit();
578 ItemKind::Mod(_) => {
579 // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
580 attr::first_attr_value_str_by_name(&item.attrs, sym::path);
582 ItemKind::Union(ref vdata, _) => {
583 if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
584 self.err_handler().span_err(item.span,
585 "tuple and unit unions are not permitted");
587 if vdata.fields().is_empty() {
588 self.err_handler().span_err(item.span,
589 "unions cannot have zero fields");
595 visit::walk_item(self, item)
598 fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
600 ForeignItemKind::Fn(ref decl, _) => {
601 self.check_fn_decl(decl);
602 Self::check_decl_no_pat(decl, |span, _| {
603 struct_span_err!(self.session, span, E0130,
604 "patterns aren't allowed in foreign function declarations")
605 .span_label(span, "pattern not allowed in foreign function").emit();
608 ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
611 visit::walk_foreign_item(self, fi)
614 // Mirrors `visit::walk_generic_args`, but tracks relevant state.
615 fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
616 match *generic_args {
617 GenericArgs::AngleBracketed(ref data) => {
618 walk_list!(self, visit_generic_arg, &data.args);
619 validate_generics_order(
622 data.args.iter().map(|arg| {
624 GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
625 GenericArg::Type(..) => ParamKindOrd::Type,
626 GenericArg::Const(..) => ParamKindOrd::Const,
627 }, None, arg.span(), None)
629 GenericPosition::Arg,
633 // Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
634 // are allowed to contain nested `impl Trait`.
635 self.with_impl_trait(None, |this| {
636 walk_list!(this, visit_assoc_ty_constraint_from_generic_args,
640 GenericArgs::Parenthesized(ref data) => {
641 walk_list!(self, visit_ty, &data.inputs);
642 if let Some(ref type_) = data.output {
643 // `-> Foo` syntax is essentially an associated type binding,
644 // so it is also allowed to contain nested `impl Trait`.
645 self.with_impl_trait(None, |this| this.visit_ty(type_));
651 fn visit_generics(&mut self, generics: &'a Generics) {
652 let mut prev_ty_default = None;
653 for param in &generics.params {
654 if let GenericParamKind::Type { ref default, .. } = param.kind {
655 if default.is_some() {
656 prev_ty_default = Some(param.ident.span);
657 } else if let Some(span) = prev_ty_default {
659 .span_err(span, "type parameters with a default must be trailing");
665 validate_generics_order(
668 generics.params.iter().map(|param| {
669 let ident = Some(param.ident.to_string());
670 let (kind, ident) = match ¶m.kind {
671 GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
672 GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
673 GenericParamKind::Const { ref ty } => {
674 let ty = pprust::ty_to_string(ty);
675 (ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
678 (kind, Some(&*param.bounds), param.ident.span, ident)
680 GenericPosition::Param,
684 for predicate in &generics.where_clause.predicates {
685 if let WherePredicate::EqPredicate(ref predicate) = *predicate {
687 .span_err(predicate.span, "equality constraints are not yet \
688 supported in where clauses (see #20041)");
692 visit::walk_generics(self, generics)
695 fn visit_generic_param(&mut self, param: &'a GenericParam) {
696 if let GenericParamKind::Lifetime { .. } = param.kind {
697 self.check_lifetime(param.ident);
699 visit::walk_generic_param(self, param);
702 fn visit_pat(&mut self, pat: &'a Pat) {
704 PatKind::Lit(ref expr) => {
705 self.check_expr_within_pat(expr, false);
707 PatKind::Range(ref start, ref end, _) => {
708 self.check_expr_within_pat(start, true);
709 self.check_expr_within_pat(end, true);
714 visit::walk_pat(self, pat)
717 fn visit_where_predicate(&mut self, p: &'a WherePredicate) {
718 if let &WherePredicate::BoundPredicate(ref bound_predicate) = p {
719 // A type binding, eg `for<'c> Foo: Send+Clone+'c`
720 self.check_late_bound_lifetime_defs(&bound_predicate.bound_generic_params);
722 visit::walk_where_predicate(self, p);
725 fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
726 self.check_late_bound_lifetime_defs(&t.bound_generic_params);
727 visit::walk_poly_trait_ref(self, t, m);
730 fn visit_variant_data(&mut self, s: &'a VariantData) {
731 self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
734 fn visit_enum_def(&mut self, enum_definition: &'a EnumDef,
735 generics: &'a Generics, item_id: NodeId, _: Span) {
736 self.with_banned_assoc_ty_bound(
737 |this| visit::walk_enum_def(this, enum_definition, generics, item_id))
740 fn visit_impl_item(&mut self, ii: &'a ImplItem) {
741 if let ImplItemKind::Method(ref sig, _) = ii.kind {
742 self.check_fn_decl(&sig.decl);
744 visit::walk_impl_item(self, ii);
747 fn visit_trait_item(&mut self, ti: &'a TraitItem) {
748 self.invalid_visibility(&ti.vis, None);
749 visit::walk_trait_item(self, ti);
753 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut lint::LintBuffer) -> bool {
754 let mut validator = AstValidator {
756 has_proc_macro_decls: false,
757 outer_impl_trait: None,
758 is_impl_trait_banned: false,
759 is_assoc_ty_bound_banned: false,
762 visit::walk_crate(&mut validator, krate);
764 validator.has_proc_macro_decls