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;
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::parse::token;
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_label(&self, label: Ident, span: Span, id: NodeId) {
|
|
|
|
if label.name == keywords::StaticLifetime.name() {
|
|
|
|
self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name));
|
|
|
|
}
|
2016-11-17 14:04:20 +00:00
|
|
|
if label.name == "'_" {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE,
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
format!("invalid label name `{}`", label.name));
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
|
|
|
|
fn invalid_visibility(&self, vis: &Visibility, span: Span, note: Option<&str>) {
|
|
|
|
if vis != &Visibility::Inherited {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut err = struct_span_err!(self.session,
|
|
|
|
span,
|
|
|
|
E0449,
|
2016-05-22 18:07:28 +03:00
|
|
|
"unnecessary visibility qualifier");
|
2016-09-26 16:05:46 -07:00
|
|
|
if vis == &Visibility::Public {
|
2017-05-04 14:17:23 +02:00
|
|
|
err.span_label(span, "`pub` not needed here");
|
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
|
|
|
|
|
|
|
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
|
|
|
|
for arg in &decl.inputs {
|
|
|
|
match arg.pat.node {
|
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
|
|
|
|
PatKind::Wild => {}
|
|
|
|
PatKind::Ident(..) => report_err(arg.pat.span, true),
|
|
|
|
_ => report_err(arg.pat.span, false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 14:33:35 -07:00
|
|
|
|
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
|
|
|
|
|
|
|
fn no_questions_in_bounds(&self, bounds: &TyParamBounds, where_: &str, is_trait: bool) {
|
|
|
|
for bound in bounds {
|
|
|
|
if let TraitTyParamBound(ref poly, TraitBoundModifier::Maybe) = *bound {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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_lifetime(&mut self, lt: &'a Lifetime) {
|
2016-11-17 14:04:20 +00:00
|
|
|
if lt.name == "'_" {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.session.add_lint(lint::builtin::LIFETIME_UNDERSCORE,
|
|
|
|
lt.id,
|
|
|
|
lt.span,
|
|
|
|
format!("invalid lifetime name `{}`", lt.name));
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_lifetime(self, lt)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_expr(&mut self, expr: &'a Expr) {
|
2016-05-22 17:51:22 +03:00
|
|
|
match expr.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
ExprKind::While(.., Some(ident)) |
|
2016-07-21 07:01:14 +05:30
|
|
|
ExprKind::Loop(_, Some(ident)) |
|
2016-08-26 19:23:42 +03:00
|
|
|
ExprKind::WhileLet(.., Some(ident)) |
|
|
|
|
ExprKind::ForLoop(.., Some(ident)) |
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 15:15:06 -07:00
|
|
|
ExprKind::Break(Some(ident), _) |
|
2016-07-21 07:01:14 +05:30
|
|
|
ExprKind::Continue(Some(ident)) => {
|
2016-05-22 17:51:22 +03:00
|
|
|
self.check_label(ident.node, ident.span, expr.id);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
self.check_decl_no_pat(&bfty.decl, |span, _| {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut err = struct_span_err!(self.session,
|
|
|
|
span,
|
|
|
|
E0561,
|
|
|
|
"patterns aren't allowed in function pointer \
|
|
|
|
types");
|
|
|
|
err.span_note(span,
|
|
|
|
"this is a recent error, see issue #35203 for more details");
|
2016-07-17 00:15:15 +03:00
|
|
|
err.emit();
|
|
|
|
});
|
|
|
|
}
|
2017-01-17 10:41:44 +03:00
|
|
|
TyKind::TraitObject(ref bounds) => {
|
2017-01-24 17:17:06 +02:00
|
|
|
let mut any_lifetime_bounds = false;
|
|
|
|
for bound in bounds {
|
|
|
|
if let RegionTyParamBound(ref lifetime) = *bound {
|
|
|
|
if any_lifetime_bounds {
|
|
|
|
span_err!(self.session, lifetime.span, E0226,
|
|
|
|
"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()
|
|
|
|
.any(|b| if let TraitTyParamBound(..) = *b { true } else { false }) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_path(&mut self, path: &'a Path, id: NodeId) {
|
2016-12-05 03:51:11 +00:00
|
|
|
if path.segments.len() >= 2 && path.is_global() {
|
|
|
|
let ident = path.segments[1].identifier;
|
2016-05-22 18:07:28 +03:00
|
|
|
if token::Ident(ident).is_path_segment_keyword() {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.session.add_lint(lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH,
|
|
|
|
id,
|
|
|
|
path.span,
|
|
|
|
format!("global paths cannot start with `{}`", ident));
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_path(self, path)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
ItemKind::Use(ref view_path) => {
|
|
|
|
let path = view_path.node.path();
|
2016-12-10 06:45:58 +00:00
|
|
|
if path.segments.iter().any(|segment| segment.parameters.is_some()) {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.err_handler()
|
|
|
|
.span_err(path.span, "type or lifetime parameters in import path");
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
ItemKind::Impl(.., Some(..), _, ref impl_items) => {
|
2016-05-22 18:07:28 +03:00
|
|
|
self.invalid_visibility(&item.vis, item.span, None);
|
|
|
|
for impl_item in impl_items {
|
|
|
|
self.invalid_visibility(&impl_item.vis, impl_item.span, None);
|
2016-08-07 14:33:35 -07:00
|
|
|
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
|
2016-08-10 16:20:12 -07:00
|
|
|
self.check_trait_fn_not_const(sig.constness);
|
2016-08-07 14:33:35 -07:00
|
|
|
}
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
ItemKind::Impl(.., None, _, _) => {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.invalid_visibility(&item.vis,
|
|
|
|
item.span,
|
|
|
|
Some("place qualifiers on individual impl items instead"));
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
ItemKind::DefaultImpl(..) => {
|
|
|
|
self.invalid_visibility(&item.vis, item.span, None);
|
|
|
|
}
|
|
|
|
ItemKind::ForeignMod(..) => {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.invalid_visibility(&item.vis,
|
|
|
|
item.span,
|
|
|
|
Some("place qualifiers on individual foreign items \
|
|
|
|
instead"));
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
ItemKind::Enum(ref def, _) => {
|
|
|
|
for variant in &def.variants {
|
|
|
|
for field in variant.node.data.fields() {
|
|
|
|
self.invalid_visibility(&field.vis, field.span, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 03:33:36 +03:00
|
|
|
ItemKind::Trait(.., ref bounds, ref trait_items) => {
|
|
|
|
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 {
|
2016-08-10 16:20:12 -07:00
|
|
|
self.check_trait_fn_not_const(sig.constness);
|
2016-10-22 03:33:36 +03:00
|
|
|
if block.is_none() {
|
|
|
|
self.check_decl_no_pat(&sig.decl, |span, _| {
|
|
|
|
self.session.add_lint(lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
|
|
|
|
trait_item.id, span,
|
|
|
|
"patterns aren't allowed in methods \
|
|
|
|
without bodies".to_string());
|
|
|
|
});
|
|
|
|
}
|
2016-08-07 14:33:35 -07: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-03-03 09:23:59 +00:00
|
|
|
if item.attrs.iter().any(|attr| attr.check_name("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";
|
|
|
|
self.session.add_lint(lint, item.id, item.span, msg.to_string());
|
|
|
|
}
|
2016-08-12 08:15:40 +00:00
|
|
|
}
|
2016-07-17 00:15:15 +03:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
|
|
|
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, _) => {
|
2016-07-17 00:15:15 +03:00
|
|
|
self.check_decl_no_pat(decl, |span, is_recent| {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut err = struct_span_err!(self.session,
|
|
|
|
span,
|
|
|
|
E0130,
|
|
|
|
"patterns aren't allowed in foreign function \
|
|
|
|
declarations");
|
2017-05-04 14:17:23 +02:00
|
|
|
err.span_label(span, "pattern not allowed in foreign function");
|
2016-07-17 00:15:15 +03:00
|
|
|
if is_recent {
|
2016-07-21 07:01:14 +05:30
|
|
|
err.span_note(span,
|
|
|
|
"this is a recent error, see issue #35203 for more details");
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
2016-07-17 00:15:15 +03:00
|
|
|
err.emit();
|
|
|
|
});
|
2016-07-17 00:15:15 +03:00
|
|
|
}
|
|
|
|
ForeignItemKind::Static(..) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_foreign_item(self, fi)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_vis(&mut self, vis: &'a Visibility) {
|
2016-05-22 18:07:28 +03:00
|
|
|
match *vis {
|
2016-07-21 07:01:14 +05:30
|
|
|
Visibility::Restricted { ref path, .. } => {
|
2016-12-10 06:45:58 +00:00
|
|
|
if !path.segments.iter().all(|segment| segment.parameters.is_none()) {
|
2016-07-21 07:01:14 +05:30
|
|
|
self.err_handler()
|
|
|
|
.span_err(path.span, "type or lifetime parameters in visibility path");
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_vis(self, vis)
|
|
|
|
}
|
2017-01-17 21:18:29 +03:00
|
|
|
|
|
|
|
fn visit_generics(&mut self, g: &'a Generics) {
|
|
|
|
let mut seen_default = None;
|
|
|
|
for ty_param in &g.ty_params {
|
|
|
|
if ty_param.default.is_some() {
|
|
|
|
seen_default = Some(ty_param.span);
|
|
|
|
} else if let Some(span) = seen_default {
|
|
|
|
self.err_handler()
|
|
|
|
.span_err(span, "type parameters with a default must be trailing");
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for predicate in &g.where_clause.predicates {
|
|
|
|
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
|
|
|
|
self.err_handler().span_err(predicate.span, "equality constraints are not yet \
|
|
|
|
supported in where clauses (#20041)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visit::walk_generics(self, g)
|
|
|
|
}
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_crate(session: &Session, krate: &Crate) {
|
2016-03-06 15:54:44 +03:00
|
|
|
visit::walk_crate(&mut AstValidator { session: session }, krate)
|
2016-05-22 17:51:22 +03:00
|
|
|
}
|