2016-05-22 17:51:22 +03:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
// Validate AST before lowering it to HIR
|
2016-05-22 17:51:22 +03:00
|
|
|
//
|
|
|
|
// This pass is supposed to catch things that fit into AST data structures,
|
|
|
|
// but not permitted by the language. It runs after expansion when AST is frozen,
|
|
|
|
// so it can check for erroneous constructions produced by syntax extensions.
|
|
|
|
// This pass is supposed to perform only simple checks not requiring name resolution
|
|
|
|
// or type checking or some other kind of complex analysis.
|
|
|
|
|
|
|
|
use rustc::lint;
|
|
|
|
use rustc::session::Session;
|
|
|
|
use syntax::ast::*;
|
2016-08-12 08:15:40 +00:00
|
|
|
use syntax::attr;
|
2016-08-10 16:20:12 -07:00
|
|
|
use syntax::codemap::Spanned;
|
2017-12-07 04:01:59 -05:00
|
|
|
use syntax::symbol::keywords;
|
2016-05-22 17:51:22 +03:00
|
|
|
use syntax::visit::{self, Visitor};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
|
|
|
use errors;
|
2016-05-22 17:51:22 +03:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
struct AstValidator<'a> {
|
2016-05-22 17:51:22 +03:00
|
|
|
session: &'a Session,
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
impl<'a> AstValidator<'a> {
|
2016-05-22 17:51:22 +03:00
|
|
|
fn err_handler(&self) -> &errors::Handler {
|
|
|
|
&self.session.parse_sess.span_diagnostic
|
|
|
|
}
|
|
|
|
|
2018-03-19 03:54:56 +03:00
|
|
|
fn check_lifetime(&self, ident: Ident) {
|
2018-03-08 14:27:23 +03:00
|
|
|
let valid_names = [keywords::UnderscoreLifetime.name(),
|
|
|
|
keywords::StaticLifetime.name(),
|
|
|
|
keywords::Invalid.name()];
|
2018-05-13 16:14:43 +03:00
|
|
|
if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
|
2018-03-19 03:54:56 +03:00
|
|
|
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
|
2017-12-06 04:28:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 03:54:56 +03:00
|
|
|
fn check_label(&self, ident: Ident) {
|
2018-05-13 16:14:43 +03:00
|
|
|
if ident.without_first_quote().is_reserved() {
|
2018-03-19 03:54:56 +03:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
|
2017-11-03 19:17:54 +00:00
|
|
|
fn invalid_non_exhaustive_attribute(&self, variant: &Variant) {
|
2017-12-26 16:52:27 +09:00
|
|
|
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
|
2017-11-03 19:17:54 +00:00
|
|
|
if has_non_exhaustive {
|
|
|
|
self.err_handler().span_err(variant.span,
|
|
|
|
"#[non_exhaustive] is not yet supported on variants");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 14:12:09 +09:00
|
|
|
fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
|
|
|
|
if vis.node != VisibilityKind::Inherited {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut err = struct_span_err!(self.session,
|
2018-01-29 14:12:09 +09:00
|
|
|
vis.span,
|
2016-07-21 07:01:14 +05:30
|
|
|
E0449,
|
2016-05-22 18:07:28 +03:00
|
|
|
"unnecessary visibility qualifier");
|
2018-01-29 14:12:09 +09:00
|
|
|
if vis.node == VisibilityKind::Public {
|
2018-03-19 16:44:58 +05:30
|
|
|
err.span_label(vis.span, "`pub` not permitted here because it's implied");
|
2016-09-26 16:05:46 -07:00
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
if let Some(note) = note {
|
2016-09-26 16:05:46 -07:00
|
|
|
err.note(note);
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
2016-07-17 00:15:15 +03:00
|
|
|
|
2017-11-05 05:08:41 +03:00
|
|
|
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
|
2016-07-17 00:15:15 +03:00
|
|
|
for arg in &decl.inputs {
|
|
|
|
match arg.pat.node {
|
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
|
|
|
|
PatKind::Wild => {}
|
2017-11-05 05:08:41 +03:00
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), _, None) =>
|
|
|
|
report_err(arg.pat.span, true),
|
|
|
|
_ => report_err(arg.pat.span, false),
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 14:33:35 -07:00
|
|
|
|
2018-06-18 21:18:10 -07:00
|
|
|
fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
|
|
|
|
if asyncness.is_async() {
|
|
|
|
struct_span_err!(self.session, span, E0706,
|
|
|
|
"trait fns cannot be declared `async`").emit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 16:20:12 -07:00
|
|
|
fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
|
|
|
|
match constness.node {
|
2016-08-07 14:33:35 -07:00
|
|
|
Constness::Const => {
|
2016-08-10 16:20:12 -07:00
|
|
|
struct_span_err!(self.session, constness.span, E0379,
|
|
|
|
"trait fns cannot be declared const")
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(constness.span, "trait fns cannot be const")
|
2016-08-07 14:33:35 -07:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 03:33:36 +03:00
|
|
|
|
2018-06-14 12:08:58 +01:00
|
|
|
fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
|
2016-10-22 03:33:36 +03:00
|
|
|
for bound in bounds {
|
2018-06-14 12:23:46 +01:00
|
|
|
if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
|
2016-10-22 03:33:36 +03:00
|
|
|
let mut err = self.err_handler().struct_span_err(poly.span,
|
|
|
|
&format!("`?Trait` is not permitted in {}", where_));
|
|
|
|
if is_trait {
|
|
|
|
err.note(&format!("traits are `?{}` by default", poly.trait_ref.path));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 18:26:04 +00:00
|
|
|
|
2018-04-10 02:08:47 +03:00
|
|
|
/// matches '-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus),
|
2017-08-13 16:59:54 +03:00
|
|
|
/// or path for ranges.
|
|
|
|
///
|
|
|
|
/// FIXME: do we want to allow expr -> pattern conversion to create path expressions?
|
|
|
|
/// That means making this work:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore (FIXME)
|
|
|
|
/// struct S;
|
|
|
|
/// macro_rules! m {
|
|
|
|
/// ($a:expr) => {
|
|
|
|
/// let $a = S;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// m!(S);
|
|
|
|
/// ```
|
|
|
|
fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
|
2017-06-24 18:26:04 +00:00
|
|
|
match expr.node {
|
2017-08-13 16:59:54 +03:00
|
|
|
ExprKind::Lit(..) => {}
|
|
|
|
ExprKind::Path(..) if allow_paths => {}
|
2017-06-24 18:26:04 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref inner)
|
|
|
|
if match inner.node { ExprKind::Lit(_) => true, _ => false } => {}
|
|
|
|
_ => self.err_handler().span_err(expr.span, "arbitrary expressions aren't allowed \
|
|
|
|
in patterns")
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 11:22:24 +01:00
|
|
|
|
2018-05-27 20:07:09 +01:00
|
|
|
fn check_late_bound_lifetime_defs(&self, params: &Vec<GenericParam>) {
|
2018-05-26 19:16:21 +01:00
|
|
|
// Check only lifetime parameters are present and that the lifetime
|
|
|
|
// parameters that are present have no bounds.
|
2018-05-27 16:56:01 +01:00
|
|
|
let non_lt_param_spans: Vec<_> = params.iter().filter_map(|param| match param.kind {
|
2018-05-28 13:33:28 +01:00
|
|
|
GenericParamKind::Lifetime { .. } => {
|
|
|
|
if !param.bounds.is_empty() {
|
|
|
|
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
|
2018-05-27 16:56:01 +01:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(spans, "lifetime bounds cannot be used in this context");
|
2018-03-06 11:22:24 +01:00
|
|
|
}
|
2018-05-26 19:16:21 +01:00
|
|
|
None
|
2018-03-06 11:22:24 +01:00
|
|
|
}
|
2018-05-26 19:16:21 +01:00
|
|
|
_ => Some(param.ident.span),
|
|
|
|
}).collect();
|
2018-05-27 16:56:01 +01:00
|
|
|
if !non_lt_param_spans.is_empty() {
|
|
|
|
self.err_handler().span_err(non_lt_param_spans,
|
2018-05-26 19:16:21 +01:00
|
|
|
"only lifetime parameters can be used in this context");
|
2018-03-06 11:22:24 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
impl<'a> Visitor<'a> for AstValidator<'a> {
|
|
|
|
fn visit_expr(&mut self, expr: &'a Expr) {
|
2016-05-22 17:51:22 +03:00
|
|
|
match expr.node {
|
2018-01-13 23:13:49 +03:00
|
|
|
ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
|
|
|
|
span_err!(self.session, expr.span, E0472, "asm! is unsupported on this target");
|
|
|
|
}
|
2018-05-24 17:34:09 -04:00
|
|
|
ExprKind::ObsoleteInPlace(..) => {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(expr.span, "emplacement syntax is obsolete (for now, anyway)")
|
2018-05-25 04:46:42 -04:00
|
|
|
.note("for more information, see \
|
|
|
|
<https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>")
|
2018-05-24 17:34:09 -04:00
|
|
|
.emit();
|
|
|
|
}
|
2016-05-22 17:51:22 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_expr(self, expr)
|
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'a Ty) {
|
2016-07-17 00:15:15 +03:00
|
|
|
match ty.node {
|
|
|
|
TyKind::BareFn(ref bfty) => {
|
2017-11-05 05:08:41 +03:00
|
|
|
self.check_decl_no_pat(&bfty.decl, |span, _| {
|
2017-11-05 04:22:18 +03:00
|
|
|
struct_span_err!(self.session, span, E0561,
|
|
|
|
"patterns aren't allowed in function pointer types").emit();
|
2016-07-17 00:15:15 +03:00
|
|
|
});
|
2018-03-06 11:22:24 +01:00
|
|
|
self.check_late_bound_lifetime_defs(&bfty.generic_params);
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
2017-10-10 17:33:19 +03:00
|
|
|
TyKind::TraitObject(ref bounds, ..) => {
|
2017-01-24 17:17:06 +02:00
|
|
|
let mut any_lifetime_bounds = false;
|
|
|
|
for bound in bounds {
|
2018-06-14 12:23:46 +01:00
|
|
|
if let GenericBound::Outlives(ref lifetime) = *bound {
|
2017-01-24 17:17:06 +02:00
|
|
|
if any_lifetime_bounds {
|
2018-03-19 03:54:56 +03:00
|
|
|
span_err!(self.session, lifetime.ident.span, E0226,
|
2017-01-24 17:17:06 +02:00
|
|
|
"only a single explicit lifetime bound is permitted");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
any_lifetime_bounds = true;
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 03:33:36 +03:00
|
|
|
self.no_questions_in_bounds(bounds, "trait object types", false);
|
|
|
|
}
|
2017-01-17 21:18:29 +03:00
|
|
|
TyKind::ImplTrait(ref bounds) => {
|
|
|
|
if !bounds.iter()
|
2018-06-14 12:23:46 +01:00
|
|
|
.any(|b| if let GenericBound::Trait(..) = *b { true } else { false }) {
|
2017-01-17 21:18:29 +03:00
|
|
|
self.err_handler().span_err(ty.span, "at least one trait must be specified");
|
|
|
|
}
|
|
|
|
}
|
2016-07-17 00:15:15 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_ty(self, ty)
|
|
|
|
}
|
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
|
|
|
|
// Check if the path in this `use` is not generic, such as `use foo::bar<T>;` While this
|
|
|
|
// can't happen normally thanks to the parser, a generic might sneak in if the `use` is
|
|
|
|
// built using a macro.
|
|
|
|
//
|
|
|
|
// macro_use foo {
|
|
|
|
// ($p:path) => { use $p; }
|
|
|
|
// }
|
|
|
|
// foo!(bar::baz<T>);
|
|
|
|
use_tree.prefix.segments.iter().find(|segment| {
|
2018-02-23 17:48:54 +00:00
|
|
|
segment.args.is_some()
|
2017-09-26 23:04:00 +02:00
|
|
|
}).map(|segment| {
|
2018-02-23 17:48:54 +00:00
|
|
|
self.err_handler().span_err(segment.args.as_ref().unwrap().span(),
|
2017-09-26 23:04:00 +02:00
|
|
|
"generic arguments in import path");
|
|
|
|
});
|
|
|
|
|
|
|
|
visit::walk_use_tree(self, use_tree, id);
|
|
|
|
}
|
|
|
|
|
2018-01-16 01:44:32 +03:00
|
|
|
fn visit_label(&mut self, label: &'a Label) {
|
2018-03-19 03:54:56 +03:00
|
|
|
self.check_label(label.ident);
|
2018-01-16 01:44:32 +03:00
|
|
|
visit::walk_label(self, label);
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:52:25 -05:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
|
2018-03-19 03:54:56 +03:00
|
|
|
self.check_lifetime(lifetime.ident);
|
2017-12-07 03:52:25 -05:00
|
|
|
visit::walk_lifetime(self, lifetime);
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_item(&mut self, item: &'a Item) {
|
2016-05-22 18:07:28 +03:00
|
|
|
match item.node {
|
2017-12-02 22:15:03 +03:00
|
|
|
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
|
2018-01-29 14:12:09 +09:00
|
|
|
self.invalid_visibility(&item.vis, None);
|
2018-01-13 19:26:49 +03:00
|
|
|
if ty.node == TyKind::Err {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
|
|
|
|
.help("use `auto trait Trait {}` instead").emit();
|
|
|
|
}
|
2017-12-02 22:15:03 +03:00
|
|
|
if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
|
|
|
|
span_err!(self.session, item.span, E0198, "negative impls cannot be unsafe");
|
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
for impl_item in impl_items {
|
2018-01-29 14:12:09 +09:00
|
|
|
self.invalid_visibility(&impl_item.vis, None);
|
2016-08-07 14:33:35 -07:00
|
|
|
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
|
2018-05-16 22:55:18 -07:00
|
|
|
self.check_trait_fn_not_const(sig.header.constness);
|
2016-08-07 14:33:35 -07:00
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-02 22:15:03 +03:00
|
|
|
ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.invalid_visibility(&item.vis,
|
|
|
|
Some("place qualifiers on individual impl items instead"));
|
2017-12-02 22:15:03 +03:00
|
|
|
if unsafety == Unsafety::Unsafe {
|
|
|
|
span_err!(self.session, item.span, E0197, "inherent impls cannot be unsafe");
|
|
|
|
}
|
|
|
|
if polarity == ImplPolarity::Negative {
|
|
|
|
self.err_handler().span_err(item.span, "inherent impls cannot be negative");
|
|
|
|
}
|
|
|
|
if defaultness == Defaultness::Default {
|
2018-03-26 00:34:58 -04:00
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(item.span, "inherent impls cannot be default")
|
2018-04-16 16:56:24 -04:00
|
|
|
.note("only trait implementations may be annotated with default").emit();
|
2017-12-02 22:15:03 +03:00
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
ItemKind::ForeignMod(..) => {
|
2018-01-29 14:12:09 +09:00
|
|
|
self.invalid_visibility(
|
|
|
|
&item.vis,
|
|
|
|
Some("place qualifiers on individual foreign items instead"),
|
|
|
|
);
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
2017-12-07 03:52:25 -05:00
|
|
|
ItemKind::Enum(ref def, _) => {
|
2016-05-22 18:07:28 +03:00
|
|
|
for variant in &def.variants {
|
2017-11-03 19:17:54 +00:00
|
|
|
self.invalid_non_exhaustive_attribute(variant);
|
2016-05-22 18:07:28 +03:00
|
|
|
for field in variant.node.data.fields() {
|
2018-01-29 14:12:09 +09:00
|
|
|
self.invalid_visibility(&field.vis, None);
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 16:03:03 -02:00
|
|
|
ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
|
|
|
|
if is_auto == IsAuto::Yes {
|
|
|
|
// Auto traits cannot have generics, super traits nor contain items.
|
2018-05-26 23:21:08 +01:00
|
|
|
if !generics.params.is_empty() {
|
2017-12-03 20:07:50 -02:00
|
|
|
struct_span_err!(self.session, item.span, E0567,
|
2017-12-04 20:55:14 -02:00
|
|
|
"auto traits cannot have generic parameters").emit();
|
2017-10-15 16:03:03 -02:00
|
|
|
}
|
|
|
|
if !bounds.is_empty() {
|
2017-12-03 20:07:50 -02:00
|
|
|
struct_span_err!(self.session, item.span, E0568,
|
2017-12-04 20:55:14 -02:00
|
|
|
"auto traits cannot have super traits").emit();
|
2017-10-15 16:03:03 -02:00
|
|
|
}
|
|
|
|
if !trait_items.is_empty() {
|
2017-12-03 20:07:50 -02:00
|
|
|
struct_span_err!(self.session, item.span, E0380,
|
2017-12-04 20:55:14 -02:00
|
|
|
"auto traits cannot have methods or associated items").emit();
|
2017-10-15 16:03:03 -02:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 03:33:36 +03:00
|
|
|
self.no_questions_in_bounds(bounds, "supertraits", true);
|
2016-08-07 14:33:35 -07:00
|
|
|
for trait_item in trait_items {
|
2016-10-22 03:33:36 +03:00
|
|
|
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
|
2018-06-18 21:18:10 -07:00
|
|
|
self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness);
|
2018-05-16 22:55:18 -07:00
|
|
|
self.check_trait_fn_not_const(sig.header.constness);
|
2016-10-22 03:33:36 +03:00
|
|
|
if block.is_none() {
|
2017-11-05 05:08:41 +03:00
|
|
|
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
|
|
|
|
if mut_ident {
|
|
|
|
self.session.buffer_lint(
|
|
|
|
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
|
|
|
|
trait_item.id, span,
|
|
|
|
"patterns aren't allowed in methods without bodies");
|
|
|
|
} else {
|
|
|
|
struct_span_err!(self.session, span, E0642,
|
|
|
|
"patterns aren't allowed in methods without bodies").emit();
|
|
|
|
}
|
2016-10-22 03:33:36 +03:00
|
|
|
});
|
|
|
|
}
|
2016-08-07 14:33:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
ItemKind::TraitAlias(Generics { ref params, .. }, ..) => {
|
|
|
|
for param in params {
|
2018-05-26 19:16:21 +01:00
|
|
|
match param.kind {
|
2018-05-27 20:07:09 +01:00
|
|
|
GenericParamKind::Lifetime { .. } => {}
|
2018-05-28 13:33:28 +01:00
|
|
|
GenericParamKind::Type { ref default, .. } => {
|
|
|
|
if !param.bounds.is_empty() {
|
2018-05-27 16:56:01 +01:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(param.ident.span, "type parameters on the left \
|
|
|
|
side of a trait alias cannot be bounded");
|
2018-05-26 19:16:21 +01:00
|
|
|
}
|
|
|
|
if !default.is_none() {
|
2018-05-27 16:56:01 +01:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(param.ident.span, "type parameters on the left \
|
|
|
|
side of a trait alias cannot have defaults");
|
2018-05-26 19:16:21 +01:00
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
}
|
2017-10-02 12:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 08:15:40 +00:00
|
|
|
ItemKind::Mod(_) => {
|
|
|
|
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
|
|
|
|
attr::first_attr_value_str_by_name(&item.attrs, "path");
|
2017-12-26 16:52:27 +09:00
|
|
|
if attr::contains_name(&item.attrs, "warn_directory_ownership") {
|
2016-11-14 09:31:03 +00:00
|
|
|
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
|
|
|
|
let msg = "cannot declare a new module at this location";
|
2017-07-26 21:51:09 -07:00
|
|
|
self.session.buffer_lint(lint, item.id, item.span, msg);
|
2016-11-14 09:31:03 +00:00
|
|
|
}
|
2016-08-12 08:15:40 +00:00
|
|
|
}
|
2017-12-07 03:52:25 -05:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
2016-07-17 00:15:15 +03:00
|
|
|
if !vdata.is_struct() {
|
|
|
|
self.err_handler().span_err(item.span,
|
|
|
|
"tuple and unit unions are not permitted");
|
|
|
|
}
|
|
|
|
if vdata.fields().len() == 0 {
|
|
|
|
self.err_handler().span_err(item.span,
|
|
|
|
"unions cannot have zero fields");
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
|
2016-07-17 00:15:15 +03:00
|
|
|
match fi.node {
|
|
|
|
ForeignItemKind::Fn(ref decl, _) => {
|
2017-11-05 05:08:41 +03:00
|
|
|
self.check_decl_no_pat(decl, |span, _| {
|
2017-11-05 04:22:18 +03:00
|
|
|
struct_span_err!(self.session, span, E0130,
|
|
|
|
"patterns aren't allowed in foreign function declarations")
|
|
|
|
.span_label(span, "pattern not allowed in foreign function").emit();
|
2016-07-17 00:15:15 +03:00
|
|
|
});
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_foreign_item(self, fi)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_vis(&mut self, vis: &'a Visibility) {
|
2018-01-29 14:12:09 +09:00
|
|
|
match vis.node {
|
|
|
|
VisibilityKind::Restricted { ref path, .. } => {
|
2018-02-23 17:48:54 +00:00
|
|
|
path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
|
|
|
|
self.err_handler().span_err(segment.args.as_ref().unwrap().span(),
|
2017-07-23 20:50:56 +03:00
|
|
|
"generic arguments in visibility path");
|
|
|
|
});
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_vis(self, vis)
|
|
|
|
}
|
2017-01-17 21:18:29 +03:00
|
|
|
|
2018-05-27 16:56:01 +01:00
|
|
|
fn visit_generics(&mut self, generics: &'a Generics) {
|
2017-10-16 21:07:26 +02:00
|
|
|
let mut seen_non_lifetime_param = false;
|
2017-01-17 21:18:29 +03:00
|
|
|
let mut seen_default = None;
|
2018-05-27 16:56:01 +01:00
|
|
|
for param in &generics.params {
|
2018-05-26 19:16:21 +01:00
|
|
|
match (¶m.kind, seen_non_lifetime_param) {
|
2018-05-27 20:07:09 +01:00
|
|
|
(GenericParamKind::Lifetime { .. }, true) => {
|
2017-10-16 21:07:26 +02:00
|
|
|
self.err_handler()
|
2018-05-26 19:16:21 +01:00
|
|
|
.span_err(param.ident.span, "lifetime parameters must be leading");
|
2017-10-16 21:07:26 +02:00
|
|
|
},
|
2018-05-27 20:07:09 +01:00
|
|
|
(GenericParamKind::Lifetime { .. }, false) => {}
|
|
|
|
(GenericParamKind::Type { ref default, .. }, _) => {
|
2017-10-16 21:07:26 +02:00
|
|
|
seen_non_lifetime_param = true;
|
2018-05-26 19:16:21 +01:00
|
|
|
if default.is_some() {
|
|
|
|
seen_default = Some(param.ident.span);
|
|
|
|
} else if let Some(span) = seen_default {
|
|
|
|
self.err_handler()
|
|
|
|
.span_err(span, "type parameters with a default must be trailing");
|
|
|
|
break;
|
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 21:18:29 +03:00
|
|
|
}
|
2018-05-27 16:56:01 +01:00
|
|
|
for predicate in &generics.where_clause.predicates {
|
2017-01-17 21:18:29 +03:00
|
|
|
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
|
|
|
|
self.err_handler().span_err(predicate.span, "equality constraints are not yet \
|
|
|
|
supported in where clauses (#20041)");
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 16:56:01 +01:00
|
|
|
visit::walk_generics(self, generics)
|
2017-01-17 21:18:29 +03:00
|
|
|
}
|
2017-06-24 18:26:04 +00:00
|
|
|
|
2018-05-30 14:36:53 +03:00
|
|
|
fn visit_generic_param(&mut self, param: &'a GenericParam) {
|
2018-05-31 15:52:17 +01:00
|
|
|
if let GenericParamKind::Lifetime { .. } = param.kind {
|
|
|
|
self.check_lifetime(param.ident);
|
2018-05-30 14:36:53 +03:00
|
|
|
}
|
|
|
|
visit::walk_generic_param(self, param);
|
|
|
|
}
|
|
|
|
|
2017-06-24 18:26:04 +00:00
|
|
|
fn visit_pat(&mut self, pat: &'a Pat) {
|
|
|
|
match pat.node {
|
|
|
|
PatKind::Lit(ref expr) => {
|
2017-08-13 16:59:54 +03:00
|
|
|
self.check_expr_within_pat(expr, false);
|
2017-06-24 18:26:04 +00:00
|
|
|
}
|
|
|
|
PatKind::Range(ref start, ref end, _) => {
|
2017-08-13 16:59:54 +03:00
|
|
|
self.check_expr_within_pat(start, true);
|
|
|
|
self.check_expr_within_pat(end, true);
|
2017-06-24 18:26:04 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_pat(self, pat)
|
|
|
|
}
|
2018-03-06 11:22:24 +01:00
|
|
|
|
|
|
|
fn visit_where_predicate(&mut self, p: &'a WherePredicate) {
|
|
|
|
if let &WherePredicate::BoundPredicate(ref bound_predicate) = p {
|
|
|
|
// A type binding, eg `for<'c> Foo: Send+Clone+'c`
|
|
|
|
self.check_late_bound_lifetime_defs(&bound_predicate.bound_generic_params);
|
|
|
|
}
|
|
|
|
visit::walk_where_predicate(self, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
|
|
|
|
self.check_late_bound_lifetime_defs(&t.bound_generic_params);
|
|
|
|
visit::walk_poly_trait_ref(self, t, m);
|
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
|
|
|
|
fn visit_mac(&mut self, mac: &Spanned<Mac_>) {
|
|
|
|
// when a new macro kind is added but the author forgets to set it up for expansion
|
|
|
|
// because that's the only part that won't cause a compiler error
|
|
|
|
self.session.diagnostic()
|
|
|
|
.span_bug(mac.span, "macro invocation missed in expansion; did you forget to override \
|
|
|
|
the relevant `fold_*()` method in `PlaceholderExpander`?");
|
|
|
|
}
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 15:40:27 -08:00
|
|
|
// Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
|
|
|
|
// Nested `impl Trait` _is_ allowed in associated type position,
|
|
|
|
// e.g `impl Iterator<Item=impl Debug>`
|
|
|
|
struct NestedImplTraitVisitor<'a> {
|
|
|
|
session: &'a Session,
|
|
|
|
outer_impl_trait: Option<Span>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NestedImplTraitVisitor<'a> {
|
|
|
|
fn with_impl_trait<F>(&mut self, outer_impl_trait: Option<Span>, f: F)
|
|
|
|
where F: FnOnce(&mut NestedImplTraitVisitor<'a>)
|
|
|
|
{
|
|
|
|
let old_outer_impl_trait = self.outer_impl_trait;
|
|
|
|
self.outer_impl_trait = outer_impl_trait;
|
|
|
|
f(self);
|
|
|
|
self.outer_impl_trait = old_outer_impl_trait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
|
|
|
|
fn visit_ty(&mut self, t: &'a Ty) {
|
|
|
|
if let TyKind::ImplTrait(_) = t.node {
|
|
|
|
if let Some(outer_impl_trait) = self.outer_impl_trait {
|
|
|
|
struct_span_err!(self.session, t.span, E0666,
|
|
|
|
"nested `impl Trait` is not allowed")
|
|
|
|
.span_label(outer_impl_trait, "outer `impl Trait`")
|
2018-02-08 16:50:14 -08:00
|
|
|
.span_label(t.span, "nested `impl Trait` here")
|
2018-02-08 15:40:27 -08:00
|
|
|
.emit();
|
|
|
|
|
|
|
|
}
|
|
|
|
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t));
|
|
|
|
} else {
|
|
|
|
visit::walk_ty(self, t);
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 17:48:54 +00:00
|
|
|
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
|
|
|
|
match *generic_args {
|
2018-05-26 23:54:48 +01:00
|
|
|
GenericArgs::AngleBracketed(ref data) => {
|
2018-05-27 16:56:01 +01:00
|
|
|
data.args.iter().for_each(|arg| match arg {
|
2018-05-27 20:07:09 +01:00
|
|
|
GenericArg::Type(ty) => self.visit_ty(ty),
|
2018-05-27 16:56:01 +01:00
|
|
|
_ => {}
|
|
|
|
});
|
2018-05-26 23:54:48 +01:00
|
|
|
for type_binding in &data.bindings {
|
2018-02-08 15:40:27 -08:00
|
|
|
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
|
|
|
|
// are allowed to contain nested `impl Trait`.
|
|
|
|
self.with_impl_trait(None, |this| visit::walk_ty(this, &type_binding.ty));
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 23:54:48 +01:00
|
|
|
GenericArgs::Parenthesized(ref data) => {
|
|
|
|
for type_ in &data.inputs {
|
2018-02-08 15:40:27 -08:00
|
|
|
self.visit_ty(type_);
|
|
|
|
}
|
2018-05-26 23:54:48 +01:00
|
|
|
if let Some(ref type_) = data.output {
|
2018-02-08 15:40:27 -08:00
|
|
|
// `-> Foo` syntax is essentially an associated type binding,
|
|
|
|
// so it is also allowed to contain nested `impl Trait`.
|
|
|
|
self.with_impl_trait(None, |this| visit::walk_ty(this, type_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
|
|
|
|
fn visit_mac(&mut self, _mac: &Spanned<Mac_>) {
|
|
|
|
// covered in AstValidator
|
|
|
|
}
|
2018-02-08 15:40:27 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 16:50:14 -08:00
|
|
|
// Bans `impl Trait` in path projections like `<impl Iterator>::Item` or `Foo::Bar<impl Trait>`.
|
|
|
|
struct ImplTraitProjectionVisitor<'a> {
|
|
|
|
session: &'a Session,
|
|
|
|
is_banned: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ImplTraitProjectionVisitor<'a> {
|
|
|
|
fn with_ban<F>(&mut self, f: F)
|
|
|
|
where F: FnOnce(&mut ImplTraitProjectionVisitor<'a>)
|
|
|
|
{
|
|
|
|
let old_is_banned = self.is_banned;
|
|
|
|
self.is_banned = true;
|
|
|
|
f(self);
|
|
|
|
self.is_banned = old_is_banned;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
|
|
|
|
fn visit_ty(&mut self, t: &'a Ty) {
|
|
|
|
match t.node {
|
|
|
|
TyKind::ImplTrait(_) => {
|
|
|
|
if self.is_banned {
|
|
|
|
struct_span_err!(self.session, t.span, E0667,
|
|
|
|
"`impl Trait` is not allowed in path parameters")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TyKind::Path(ref qself, ref path) => {
|
|
|
|
// We allow these:
|
|
|
|
// - `Option<impl Trait>`
|
|
|
|
// - `option::Option<impl Trait>`
|
|
|
|
// - `option::Option<T>::Foo<impl Trait>
|
|
|
|
//
|
|
|
|
// But not these:
|
|
|
|
// - `<impl Trait>::Foo`
|
|
|
|
// - `option::Option<impl Trait>::Foo`.
|
|
|
|
//
|
|
|
|
// To implement this, we disallow `impl Trait` from `qself`
|
|
|
|
// (for cases like `<impl Trait>::Foo>`)
|
2018-02-23 17:48:54 +00:00
|
|
|
// but we allow `impl Trait` in `GenericArgs`
|
2018-02-08 16:50:14 -08:00
|
|
|
// iff there are no more PathSegments.
|
|
|
|
if let Some(ref qself) = *qself {
|
|
|
|
// `impl Trait` in `qself` is always illegal
|
|
|
|
self.with_ban(|this| this.visit_ty(&qself.ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
// Allow `impl Trait` iff we're on the final path segment
|
|
|
|
if i == (path.segments.len() - 1) {
|
|
|
|
visit::walk_path_segment(self, path.span, segment);
|
|
|
|
} else {
|
|
|
|
self.with_ban(|this|
|
|
|
|
visit::walk_path_segment(this, path.span, segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => visit::walk_ty(self, t),
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
|
|
|
|
fn visit_mac(&mut self, _mac: &Spanned<Mac_>) {
|
|
|
|
// covered in AstValidator
|
|
|
|
}
|
2018-02-08 16:50:14 -08:00
|
|
|
}
|
2018-02-08 15:40:27 -08:00
|
|
|
|
2016-05-22 17:51:22 +03:00
|
|
|
pub fn check_crate(session: &Session, krate: &Crate) {
|
2018-02-08 15:40:27 -08:00
|
|
|
visit::walk_crate(
|
|
|
|
&mut NestedImplTraitVisitor {
|
|
|
|
session,
|
|
|
|
outer_impl_trait: None,
|
|
|
|
}, krate);
|
|
|
|
|
2018-02-08 16:50:14 -08:00
|
|
|
visit::walk_crate(
|
|
|
|
&mut ImplTraitProjectionVisitor {
|
|
|
|
session,
|
|
|
|
is_banned: false,
|
|
|
|
}, krate);
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
visit::walk_crate(&mut AstValidator { session: session }, krate)
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|