2019-02-28 22:43:53 +00:00
|
|
|
// Validate AST before lowering it to HIR.
|
2016-05-22 14:51:22 +00: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.
|
|
|
|
|
2019-12-31 20:25:16 +00:00
|
|
|
use errors::{struct_span_err, Applicability, FatalError};
|
2016-05-22 14:51:22 +00:00
|
|
|
use rustc::lint;
|
|
|
|
use rustc::session::Session;
|
2019-02-05 15:52:17 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-10-15 20:48:13 +00:00
|
|
|
use rustc_parse::validate_attr;
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2019-12-24 22:38:22 +00:00
|
|
|
use std::mem;
|
2016-05-22 14:51:22 +00:00
|
|
|
use syntax::ast::*;
|
2016-08-12 08:15:40 +00:00
|
|
|
use syntax::attr;
|
2019-10-09 14:47:38 +00:00
|
|
|
use syntax::expand::is_proc_macro_attr;
|
2019-10-15 20:48:13 +00:00
|
|
|
use syntax::print::pprust;
|
2016-05-22 14:51:22 +00:00
|
|
|
use syntax::visit::{self, Visitor};
|
2019-12-31 20:25:16 +00:00
|
|
|
use syntax::walk_list;
|
2016-05-22 14:51:22 +00:00
|
|
|
|
2019-11-11 21:46:56 +00:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2020-01-05 00:29:45 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum BoundContext {
|
|
|
|
ImplTrait,
|
|
|
|
TraitBounds,
|
|
|
|
TraitObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BoundContext {
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::ImplTrait => "`impl Trait`",
|
|
|
|
Self::TraitBounds => "supertraits",
|
|
|
|
Self::TraitObject => "trait objects",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
struct AstValidator<'a> {
|
2016-05-22 14:51:22 +00:00
|
|
|
session: &'a Session,
|
2019-01-17 06:28:39 +00:00
|
|
|
has_proc_macro_decls: bool,
|
2019-01-18 11:35:14 +00:00
|
|
|
|
2019-05-05 12:06:04 +00:00
|
|
|
/// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
|
|
|
|
/// Nested `impl Trait` _is_ allowed in associated type position,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// e.g., `impl Iterator<Item = impl Debug>`.
|
2019-08-03 19:59:22 +00:00
|
|
|
outer_impl_trait: Option<Span>,
|
2019-01-18 11:35:14 +00:00
|
|
|
|
2020-01-05 00:29:45 +00:00
|
|
|
/// Tracks the context in which a bound can appear.
|
|
|
|
///
|
|
|
|
/// This is used to forbid `?const Trait` bounds in certain contexts.
|
|
|
|
bound_context_stack: Vec<Option<BoundContext>>,
|
|
|
|
|
2019-05-05 12:06:04 +00:00
|
|
|
/// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
|
|
|
|
/// or `Foo::Bar<impl Trait>`
|
2019-01-18 11:35:14 +00:00
|
|
|
is_impl_trait_banned: bool,
|
2019-02-20 21:24:32 +00:00
|
|
|
|
2019-03-21 17:55:09 +00:00
|
|
|
/// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
|
|
|
|
/// certain positions.
|
|
|
|
is_assoc_ty_bound_banned: bool,
|
|
|
|
|
2019-10-25 13:15:33 +00:00
|
|
|
lint_buffer: &'a mut lint::LintBuffer,
|
2019-05-15 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
impl<'a> AstValidator<'a> {
|
2019-01-19 03:29:26 +00:00
|
|
|
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
|
2019-06-22 23:39:13 +00:00
|
|
|
let old = mem::replace(&mut self.is_impl_trait_banned, true);
|
|
|
|
f(self);
|
|
|
|
self.is_impl_trait_banned = old;
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 17:55:09 +00:00
|
|
|
fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
|
|
|
|
let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true);
|
|
|
|
f(self);
|
|
|
|
self.is_assoc_ty_bound_banned = old;
|
|
|
|
}
|
|
|
|
|
2019-08-03 19:59:22 +00:00
|
|
|
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
|
2020-01-05 00:29:45 +00:00
|
|
|
self.bound_context_stack.push(outer.map(|_| BoundContext::ImplTrait));
|
2019-06-22 23:39:13 +00:00
|
|
|
let old = mem::replace(&mut self.outer_impl_trait, outer);
|
|
|
|
f(self);
|
|
|
|
self.outer_impl_trait = old;
|
2020-01-05 00:29:45 +00:00
|
|
|
self.bound_context_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_bound_context(&mut self, ctx: Option<BoundContext>, f: impl FnOnce(&mut Self)) {
|
|
|
|
self.bound_context_stack.push(ctx);
|
|
|
|
f(self);
|
|
|
|
self.bound_context_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn innermost_bound_context(&mut self) -> Option<BoundContext> {
|
|
|
|
self.bound_context_stack.iter().rev().find(|x| x.is_some()).copied().flatten()
|
2019-05-15 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) {
|
2019-03-21 17:55:09 +00:00
|
|
|
match constraint.kind {
|
2019-08-03 19:59:22 +00:00
|
|
|
AssocTyConstraintKind::Equality { .. } => {}
|
2019-03-21 17:55:09 +00:00
|
|
|
AssocTyConstraintKind::Bound { .. } => {
|
|
|
|
if self.is_assoc_ty_bound_banned {
|
2019-12-24 22:38:22 +00:00
|
|
|
self.err_handler().span_err(
|
|
|
|
constraint.span,
|
|
|
|
"associated type bounds are not allowed within structs, enums, or unions",
|
2019-03-21 17:55:09 +00:00
|
|
|
);
|
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
}
|
2019-03-11 14:14:24 +00:00
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
self.visit_assoc_ty_constraint(constraint);
|
2019-03-11 14:14:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Mirrors `visit::walk_ty`, but tracks relevant state.
|
2019-01-18 11:35:14 +00:00
|
|
|
fn walk_ty(&mut self, t: &'a Ty) {
|
2019-09-26 16:25:31 +00:00
|
|
|
match t.kind {
|
2019-01-18 11:35:14 +00:00
|
|
|
TyKind::ImplTrait(..) => {
|
2019-08-03 19:59:22 +00:00
|
|
|
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
2020-01-05 00:29:45 +00:00
|
|
|
TyKind::TraitObject(..) => {
|
|
|
|
self.with_bound_context(Some(BoundContext::TraitObject), |this| {
|
|
|
|
visit::walk_ty(this, t)
|
|
|
|
});
|
|
|
|
}
|
2019-01-18 11:35:14 +00:00
|
|
|
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>`)
|
|
|
|
// but we allow `impl Trait` in `GenericArgs`
|
|
|
|
// iff there are no more PathSegments.
|
|
|
|
if let Some(ref qself) = *qself {
|
|
|
|
// `impl Trait` in `qself` is always illegal
|
|
|
|
self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that there should be a call to visit_path here,
|
|
|
|
// so if any logic is added to process `Path`s a call to it should be
|
|
|
|
// added both in visit_path and here. This code mirrors visit::walk_path.
|
|
|
|
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 {
|
|
|
|
self.visit_path_segment(path.span, segment);
|
|
|
|
} else {
|
|
|
|
self.with_banned_impl_trait(|this| {
|
|
|
|
this.visit_path_segment(path.span, segment)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => visit::walk_ty(self, t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 14:51:22 +00:00
|
|
|
fn err_handler(&self) -> &errors::Handler {
|
2018-07-26 22:18:06 +00:00
|
|
|
&self.session.diagnostic()
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 00:54:56 +00:00
|
|
|
fn check_lifetime(&self, ident: Ident) {
|
2019-12-24 22:38:22 +00:00
|
|
|
let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Invalid];
|
2018-05-13 13:14:43 +00:00
|
|
|
if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
|
2018-03-19 00:54:56 +00:00
|
|
|
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
|
2017-12-06 09:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 00:54:56 +00:00
|
|
|
fn check_label(&self, ident: Ident) {
|
2018-05-13 13:14:43 +00:00
|
|
|
if ident.without_first_quote().is_reserved() {
|
2018-03-19 00:54:56 +00:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
|
2018-01-29 05:12:09 +00:00
|
|
|
fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
|
2018-03-20 22:58:25 +00:00
|
|
|
if let VisibilityKind::Inherited = vis.node {
|
2019-12-24 22:38:22 +00:00
|
|
|
return;
|
2018-03-20 22:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
let mut err =
|
|
|
|
struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier");
|
2018-07-06 20:18:38 +00:00
|
|
|
if vis.node.is_pub() {
|
2018-03-20 22:58:25 +00:00
|
|
|
err.span_label(vis.span, "`pub` not permitted here because it's implied");
|
|
|
|
}
|
|
|
|
if let Some(note) = note {
|
|
|
|
err.note(note);
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2018-03-20 22:58:25 +00:00
|
|
|
err.emit();
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2016-07-16 21:15:15 +00:00
|
|
|
|
2019-12-01 11:43:39 +00:00
|
|
|
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, bool)) {
|
|
|
|
for Param { pat, .. } in &decl.inputs {
|
|
|
|
match pat.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
|
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => {
|
|
|
|
report_err(pat.span, true)
|
|
|
|
}
|
2019-12-01 11:43:39 +00:00
|
|
|
_ => report_err(pat.span, false),
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 21:33:35 +00:00
|
|
|
|
2019-05-29 01:10:49 +00:00
|
|
|
fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
|
2018-06-19 04:18:10 +00:00
|
|
|
if asyncness.is_async() {
|
2019-11-18 20:08:03 +00:00
|
|
|
struct_span_err!(self.session, span, E0706, "trait fns cannot be declared `async`")
|
|
|
|
.note("`async` trait functions are not currently supported")
|
2019-12-24 22:38:22 +00:00
|
|
|
.note(
|
|
|
|
"consider using the `async-trait` crate: \
|
|
|
|
https://crates.io/crates/async-trait",
|
|
|
|
)
|
2019-10-29 15:48:14 +00:00
|
|
|
.emit();
|
2018-06-19 04:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:20:12 +00:00
|
|
|
fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
|
2018-09-07 13:10:16 +00:00
|
|
|
if constness.node == Constness::Const {
|
2019-12-24 22:38:22 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
constness.span,
|
|
|
|
E0379,
|
|
|
|
"trait fns cannot be declared const"
|
|
|
|
)
|
|
|
|
.span_label(constness.span, "trait fns cannot be const")
|
|
|
|
.emit();
|
2016-08-07 21:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 00:33:36 +00:00
|
|
|
|
2020-01-05 00:29:45 +00:00
|
|
|
// FIXME(ecstaticmorse): Instead, use the `bound_context_stack` to check this in
|
|
|
|
// `visit_param_bound`.
|
2018-06-14 11:08:58 +00:00
|
|
|
fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
|
2016-10-22 00:33:36 +00:00
|
|
|
for bound in bounds {
|
2018-06-14 11:23:46 +00:00
|
|
|
if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
|
2019-12-24 22:38:22 +00:00
|
|
|
let mut err = self.err_handler().struct_span_err(
|
|
|
|
poly.span,
|
|
|
|
&format!("`?Trait` is not permitted in {}", where_),
|
|
|
|
);
|
2016-10-22 00:33:36 +00:00
|
|
|
if is_trait {
|
2019-10-08 20:17:46 +00:00
|
|
|
let path_str = pprust::path_to_string(&poly.trait_ref.path);
|
|
|
|
err.note(&format!("traits are `?{}` by default", path_str));
|
2016-10-22 00:33:36 +00:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 18:26:04 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Matches `'-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus)`,
|
|
|
|
/// or paths 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);
|
|
|
|
// ```
|
2017-08-13 13:59:54 +00:00
|
|
|
fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
|
2019-09-26 13:39:48 +00:00
|
|
|
match expr.kind {
|
2019-07-24 02:01:12 +00:00
|
|
|
ExprKind::Lit(..) | ExprKind::Err => {}
|
2017-08-13 13:59:54 +00:00
|
|
|
ExprKind::Path(..) if allow_paths => {}
|
2017-06-24 18:26:04 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref inner)
|
2019-12-24 22:38:22 +00:00
|
|
|
if match inner.kind {
|
|
|
|
ExprKind::Lit(_) => true,
|
|
|
|
_ => false,
|
|
|
|
} => {}
|
|
|
|
_ => self.err_handler().span_err(
|
|
|
|
expr.span,
|
|
|
|
"arbitrary expressions aren't allowed \
|
|
|
|
in patterns",
|
|
|
|
),
|
2017-06-24 18:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-06 10:22:24 +00:00
|
|
|
|
2018-07-27 14:50:28 +00:00
|
|
|
fn check_late_bound_lifetime_defs(&self, params: &[GenericParam]) {
|
2018-05-26 18:16:21 +00:00
|
|
|
// Check only lifetime parameters are present and that the lifetime
|
|
|
|
// parameters that are present have no bounds.
|
2019-12-24 22:38:22 +00:00
|
|
|
let non_lt_param_spans: Vec<_> = params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
|
|
|
if !param.bounds.is_empty() {
|
|
|
|
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
|
|
|
|
self.err_handler()
|
|
|
|
.span_err(spans, "lifetime bounds cannot be used in this context");
|
|
|
|
}
|
|
|
|
None
|
2018-03-06 10:22:24 +00:00
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
_ => Some(param.ident.span),
|
|
|
|
})
|
|
|
|
.collect();
|
2018-05-27 15:56:01 +00:00
|
|
|
if !non_lt_param_spans.is_empty() {
|
2019-12-24 22:38:22 +00:00
|
|
|
self.err_handler().span_err(
|
|
|
|
non_lt_param_spans,
|
|
|
|
"only lifetime parameters can be used in this context",
|
|
|
|
);
|
2018-03-06 10:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 18:35:03 +00:00
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
fn check_fn_decl(&self, fn_decl: &FnDecl) {
|
2019-12-02 02:16:12 +00:00
|
|
|
match &*fn_decl.inputs {
|
2019-12-24 22:38:22 +00:00
|
|
|
[Param { ty, span, .. }] => {
|
|
|
|
if let TyKind::CVarArgs = ty.kind {
|
|
|
|
self.err_handler().span_err(
|
2019-12-02 02:16:12 +00:00
|
|
|
*span,
|
|
|
|
"C-variadic function must be declared with at least one named argument",
|
|
|
|
);
|
2019-12-24 22:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[ps @ .., _] => {
|
|
|
|
for Param { ty, span, .. } in ps {
|
|
|
|
if let TyKind::CVarArgs = ty.kind {
|
|
|
|
self.err_handler().span_err(
|
2019-12-02 02:16:12 +00:00
|
|
|
*span,
|
|
|
|
"`...` must be the last argument of a C-variadic function",
|
|
|
|
);
|
2019-12-24 22:38:22 +00:00
|
|
|
}
|
2019-12-02 02:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
fn_decl
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.flat_map(|i| i.attrs.as_ref())
|
|
|
|
.filter(|attr| {
|
|
|
|
let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn];
|
2019-11-30 01:20:07 +00:00
|
|
|
!arr.contains(&attr.name_or_empty()) && attr::is_builtin_attr(attr)
|
2019-06-09 10:58:40 +00:00
|
|
|
})
|
2019-12-24 22:38:22 +00:00
|
|
|
.for_each(|attr| {
|
|
|
|
if attr.is_doc_comment() {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"documentation comments cannot be applied to function parameters",
|
|
|
|
)
|
|
|
|
.span_label(attr.span, "doc comments are not allowed here")
|
|
|
|
.emit();
|
|
|
|
} else {
|
|
|
|
self.err_handler().span_err(
|
|
|
|
attr.span,
|
|
|
|
"allow, cfg, cfg_attr, deny, \
|
|
|
|
forbid, and warn are the only allowed built-in attributes in function parameters",
|
|
|
|
)
|
|
|
|
}
|
2019-06-09 10:58:40 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-30 17:25:44 +00:00
|
|
|
|
|
|
|
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
|
|
|
|
if let Defaultness::Default = defaultness {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 06:24:07 +00:00
|
|
|
|
|
|
|
fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) {
|
|
|
|
if body.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(sp, &format!("associated {} in `impl` without body", ctx))
|
|
|
|
.span_suggestion(
|
|
|
|
self.session.source_map().end_point(sp),
|
|
|
|
&format!("provide a definition for the {}", ctx),
|
|
|
|
sugg.to_string(),
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
2019-12-01 09:25:45 +00:00
|
|
|
|
|
|
|
fn check_impl_assoc_type_no_bounds(&self, bounds: &[GenericBound]) {
|
|
|
|
let span = match bounds {
|
|
|
|
[] => return,
|
|
|
|
[b0] => b0.span(),
|
|
|
|
[b0, .., bl] => b0.span().to(bl.span()),
|
|
|
|
};
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(span, "bounds on associated `type`s in `impl`s have no effect")
|
|
|
|
.emit();
|
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
|
|
|
|
fn check_c_varadic_type(&self, decl: &FnDecl) {
|
|
|
|
for Param { ty, span, .. } in &decl.inputs {
|
|
|
|
if let TyKind::CVarArgs = ty.kind {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
*span,
|
|
|
|
"only foreign or `unsafe extern \"C\" functions may be C-variadic",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum GenericPosition {
|
|
|
|
Param,
|
|
|
|
Arg,
|
|
|
|
}
|
2018-08-31 18:35:03 +00:00
|
|
|
|
2019-02-05 15:52:17 +00:00
|
|
|
fn validate_generics_order<'a>(
|
2019-03-30 19:57:04 +00:00
|
|
|
sess: &Session,
|
2019-02-05 15:52:17 +00:00
|
|
|
handler: &errors::Handler,
|
2019-12-24 22:38:22 +00:00
|
|
|
generics: impl Iterator<Item = (ParamKindOrd, Option<&'a [GenericBound]>, Span, Option<String>)>,
|
2019-02-05 15:52:17 +00:00
|
|
|
pos: GenericPosition,
|
|
|
|
span: Span,
|
|
|
|
) {
|
|
|
|
let mut max_param: Option<ParamKindOrd> = None;
|
|
|
|
let mut out_of_order = FxHashMap::default();
|
|
|
|
let mut param_idents = vec![];
|
2019-05-04 13:38:10 +00:00
|
|
|
let mut found_type = false;
|
|
|
|
let mut found_const = false;
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2019-03-30 19:30:36 +00:00
|
|
|
for (kind, bounds, span, ident) in generics {
|
2019-02-05 15:52:17 +00:00
|
|
|
if let Some(ident) = ident {
|
2019-03-30 19:30:36 +00:00
|
|
|
param_idents.push((kind, bounds, param_idents.len(), ident));
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
|
|
|
let max_param = &mut max_param;
|
|
|
|
match max_param {
|
|
|
|
Some(max_param) if *max_param > kind => {
|
|
|
|
let entry = out_of_order.entry(kind).or_insert((*max_param, vec![]));
|
|
|
|
entry.1.push(span);
|
|
|
|
}
|
|
|
|
Some(_) | None => *max_param = Some(kind),
|
|
|
|
};
|
2019-05-04 13:38:10 +00:00
|
|
|
match kind {
|
|
|
|
ParamKindOrd::Type => found_type = true,
|
|
|
|
ParamKindOrd::Const => found_const = true,
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut ordered_params = "<".to_string();
|
|
|
|
if !out_of_order.is_empty() {
|
2019-03-30 19:30:36 +00:00
|
|
|
param_idents.sort_by_key(|&(po, _, i, _)| (po, i));
|
2019-02-05 15:52:17 +00:00
|
|
|
let mut first = true;
|
2019-03-30 19:30:36 +00:00
|
|
|
for (_, bounds, _, ident) in param_idents {
|
2019-02-05 15:52:17 +00:00
|
|
|
if !first {
|
|
|
|
ordered_params += ", ";
|
|
|
|
}
|
2019-02-05 18:00:07 +00:00
|
|
|
ordered_params += &ident;
|
2019-03-30 19:30:36 +00:00
|
|
|
if let Some(bounds) = bounds {
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
ordered_params += ": ";
|
|
|
|
ordered_params += &pprust::bounds_to_string(&bounds);
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ordered_params += ">";
|
|
|
|
|
|
|
|
let pos_str = match pos {
|
|
|
|
GenericPosition::Param => "parameter",
|
|
|
|
GenericPosition::Arg => "argument",
|
|
|
|
};
|
|
|
|
|
2019-05-04 13:38:10 +00:00
|
|
|
for (param_ord, (max_param, spans)) in &out_of_order {
|
2019-12-24 22:38:22 +00:00
|
|
|
let mut err = handler.struct_span_err(
|
|
|
|
spans.clone(),
|
2019-02-05 15:52:17 +00:00
|
|
|
&format!(
|
|
|
|
"{} {pos}s must be declared prior to {} {pos}s",
|
|
|
|
param_ord,
|
|
|
|
max_param,
|
|
|
|
pos = pos_str,
|
2019-12-24 22:38:22 +00:00
|
|
|
),
|
|
|
|
);
|
2019-02-05 15:52:17 +00:00
|
|
|
if let GenericPosition::Param = pos {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
2019-03-30 19:57:04 +00:00
|
|
|
&format!(
|
|
|
|
"reorder the {}s: lifetimes, then types{}",
|
|
|
|
pos_str,
|
|
|
|
if sess.features_untracked().const_generics { ", then consts" } else { "" },
|
|
|
|
),
|
2019-02-05 15:52:17 +00:00
|
|
|
ordered_params.clone(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-05-04 13:38:10 +00:00
|
|
|
|
|
|
|
// FIXME(const_generics): we shouldn't have to abort here at all, but we currently get ICEs
|
|
|
|
// if we don't. Const parameters and type parameters can currently conflict if they
|
|
|
|
// are out-of-order.
|
|
|
|
if !out_of_order.is_empty() && found_type && found_const {
|
|
|
|
FatalError.raise();
|
|
|
|
}
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
impl<'a> Visitor<'a> for AstValidator<'a> {
|
2019-10-11 19:00:09 +00:00
|
|
|
fn visit_attribute(&mut self, attr: &Attribute) {
|
|
|
|
validate_attr::check_meta(&self.session.parse_sess, attr);
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'a Expr) {
|
2019-09-26 13:39:48 +00:00
|
|
|
match &expr.kind {
|
2019-06-17 05:36:45 +00:00
|
|
|
ExprKind::Closure(_, _, _, fn_decl, _, _) => {
|
|
|
|
self.check_fn_decl(fn_decl);
|
2018-01-13 20:13:49 +00:00
|
|
|
}
|
2019-06-17 05:36:45 +00:00
|
|
|
ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
expr.span,
|
|
|
|
E0472,
|
|
|
|
"asm! is unsupported on this target"
|
|
|
|
)
|
|
|
|
.emit();
|
2019-06-17 05:36:45 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2016-05-22 14:51:22 +00:00
|
|
|
|
2019-06-17 05:36:45 +00:00
|
|
|
visit::walk_expr(self, expr);
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_ty(&mut self, ty: &'a Ty) {
|
2019-09-26 16:25:31 +00:00
|
|
|
match ty.kind {
|
2016-07-16 21:15:15 +00:00
|
|
|
TyKind::BareFn(ref bfty) => {
|
2019-06-09 10:58:40 +00:00
|
|
|
self.check_fn_decl(&bfty.decl);
|
2019-10-25 13:15:33 +00:00
|
|
|
Self::check_decl_no_pat(&bfty.decl, |span, _| {
|
2019-12-24 22:38:22 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
span,
|
|
|
|
E0561,
|
|
|
|
"patterns aren't allowed in function pointer types"
|
|
|
|
)
|
|
|
|
.emit();
|
2016-07-16 21:15:15 +00:00
|
|
|
});
|
2018-03-06 10:22:24 +00:00
|
|
|
self.check_late_bound_lifetime_defs(&bfty.generic_params);
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2017-10-10 14:33:19 +00:00
|
|
|
TyKind::TraitObject(ref bounds, ..) => {
|
2017-01-24 15:17:06 +00:00
|
|
|
let mut any_lifetime_bounds = false;
|
|
|
|
for bound in bounds {
|
2018-06-14 11:23:46 +00:00
|
|
|
if let GenericBound::Outlives(ref lifetime) = *bound {
|
2017-01-24 15:17:06 +00:00
|
|
|
if any_lifetime_bounds {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
2019-12-24 22:38:22 +00:00
|
|
|
self.session,
|
|
|
|
lifetime.ident.span,
|
|
|
|
E0226,
|
|
|
|
"only a single explicit lifetime bound is permitted"
|
2019-12-31 20:25:16 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2017-01-24 15:17:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
any_lifetime_bounds = true;
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 00:33:36 +00:00
|
|
|
self.no_questions_in_bounds(bounds, "trait object types", false);
|
|
|
|
}
|
2018-06-18 14:23:13 +00:00
|
|
|
TyKind::ImplTrait(_, ref bounds) => {
|
2019-01-18 11:35:14 +00:00
|
|
|
if self.is_impl_trait_banned {
|
2019-08-03 19:59:22 +00:00
|
|
|
struct_span_err!(
|
2019-12-24 22:38:22 +00:00
|
|
|
self.session,
|
|
|
|
ty.span,
|
|
|
|
E0667,
|
2019-08-03 19:59:22 +00:00
|
|
|
"`impl Trait` is not allowed in path parameters"
|
|
|
|
)
|
|
|
|
.emit();
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 19:59:22 +00:00
|
|
|
if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
|
|
|
|
struct_span_err!(
|
2019-12-24 22:38:22 +00:00
|
|
|
self.session,
|
|
|
|
ty.span,
|
|
|
|
E0666,
|
2019-08-03 19:59:22 +00:00
|
|
|
"nested `impl Trait` is not allowed"
|
|
|
|
)
|
|
|
|
.span_label(outer_impl_trait_sp, "outer `impl Trait`")
|
|
|
|
.span_label(ty.span, "nested `impl Trait` here")
|
|
|
|
.emit();
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
2019-02-20 21:24:32 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
if !bounds
|
|
|
|
.iter()
|
|
|
|
.any(|b| if let GenericBound::Trait(..) = *b { true } else { false })
|
|
|
|
{
|
2017-01-17 18:18:29 +00:00
|
|
|
self.err_handler().span_err(ty.span, "at least one trait must be specified");
|
|
|
|
}
|
2019-02-20 21:24:32 +00:00
|
|
|
|
2019-08-03 19:59:22 +00:00
|
|
|
self.walk_ty(ty);
|
2019-02-20 21:24:32 +00:00
|
|
|
return;
|
2017-01-17 18:18:29 +00:00
|
|
|
}
|
2016-07-16 21:15:15 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:35:14 +00:00
|
|
|
self.walk_ty(ty)
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:44:32 +00:00
|
|
|
fn visit_label(&mut self, label: &'a Label) {
|
2018-03-19 00:54:56 +00:00
|
|
|
self.check_label(label.ident);
|
2018-01-15 22:44:32 +00:00
|
|
|
visit::walk_label(self, label);
|
|
|
|
}
|
|
|
|
|
2017-12-07 08:52:25 +00:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
|
2018-03-19 00:54:56 +00:00
|
|
|
self.check_lifetime(lifetime.ident);
|
2017-12-07 08:52:25 +00:00
|
|
|
visit::walk_lifetime(self, lifetime);
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_item(&mut self, item: &'a Item) {
|
2019-12-24 22:38:22 +00:00
|
|
|
if item.attrs.iter().any(|attr| is_proc_macro_attr(attr)) {
|
2019-01-17 06:28:39 +00:00
|
|
|
self.has_proc_macro_decls = true;
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2017-12-02 19:15:03 +00:00
|
|
|
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
|
2018-01-29 05:12:09 +00:00
|
|
|
self.invalid_visibility(&item.vis, None);
|
2019-09-26 16:25:31 +00:00
|
|
|
if let TyKind::Err = ty.kind {
|
2018-01-13 16:26:49 +00:00
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
|
2019-12-24 22:38:22 +00:00
|
|
|
.help("use `auto trait Trait {}` instead")
|
|
|
|
.emit();
|
2018-01-13 16:26:49 +00:00
|
|
|
}
|
2017-12-02 19:15:03 +00:00
|
|
|
if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0198,
|
|
|
|
"negative impls cannot be unsafe"
|
|
|
|
)
|
|
|
|
.emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
for impl_item in impl_items {
|
2018-01-29 05:12:09 +00:00
|
|
|
self.invalid_visibility(&impl_item.vis, None);
|
2019-12-07 23:13:59 +00:00
|
|
|
if let AssocItemKind::Fn(ref sig, _) = impl_item.kind {
|
2018-05-17 05:55:18 +00:00
|
|
|
self.check_trait_fn_not_const(sig.header.constness);
|
2019-05-29 01:10:49 +00:00
|
|
|
self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
|
2016-08-07 21:33:35 +00:00
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-02 19:15:03 +00:00
|
|
|
ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
|
2019-12-24 22:38:22 +00:00
|
|
|
self.invalid_visibility(
|
|
|
|
&item.vis,
|
|
|
|
Some("place qualifiers on individual impl items instead"),
|
|
|
|
);
|
2017-12-02 19:15:03 +00:00
|
|
|
if unsafety == Unsafety::Unsafe {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0197,
|
|
|
|
"inherent impls cannot be unsafe"
|
|
|
|
)
|
|
|
|
.emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
|
|
|
if polarity == ImplPolarity::Negative {
|
|
|
|
self.err_handler().span_err(item.span, "inherent impls cannot be negative");
|
|
|
|
}
|
|
|
|
if defaultness == Defaultness::Default {
|
2018-03-26 04:34:58 +00:00
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(item.span, "inherent impls cannot be default")
|
2019-12-24 22:38:22 +00:00
|
|
|
.note("only trait implementations may be annotated with default")
|
|
|
|
.emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2019-11-07 12:33:37 +00:00
|
|
|
ItemKind::Fn(ref sig, ref generics, _) => {
|
|
|
|
self.visit_fn_header(&sig.header);
|
|
|
|
self.check_fn_decl(&sig.decl);
|
2019-02-05 15:52:17 +00:00
|
|
|
// We currently do not permit const generics in `const fn`, as
|
|
|
|
// this is tantamount to allowing compile-time dependent typing.
|
2019-11-07 12:33:37 +00:00
|
|
|
if sig.header.constness.node == Constness::Const {
|
2019-02-05 15:52:17 +00:00
|
|
|
// Look for const generics and error if we find any.
|
|
|
|
for param in &generics.params {
|
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Const { .. } => {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
item.span,
|
|
|
|
"const parameters are not permitted in `const fn`",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
// Reject C-varadic type unless the function is `unsafe extern "C"` semantically.
|
|
|
|
match sig.header.ext {
|
2019-12-24 22:38:22 +00:00
|
|
|
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. })
|
|
|
|
| Extern::Implicit
|
|
|
|
if sig.header.unsafety == Unsafety::Unsafe => {}
|
2019-12-02 01:38:33 +00:00
|
|
|
_ => self.check_c_varadic_type(&sig.decl),
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
ItemKind::ForeignMod(..) => {
|
2018-01-29 05:12:09 +00:00
|
|
|
self.invalid_visibility(
|
|
|
|
&item.vis,
|
|
|
|
Some("place qualifiers on individual foreign items instead"),
|
|
|
|
);
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2017-12-07 08:52:25 +00:00
|
|
|
ItemKind::Enum(ref def, _) => {
|
2016-05-22 15:07:28 +00:00
|
|
|
for variant in &def.variants {
|
2019-11-07 10:26:36 +00:00
|
|
|
self.invalid_visibility(&variant.vis, None);
|
2019-08-14 00:40:21 +00:00
|
|
|
for field in variant.data.fields() {
|
2018-01-29 05:12:09 +00:00
|
|
|
self.invalid_visibility(&field.vis, None);
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 18:03:03 +00: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 22:21:08 +00:00
|
|
|
if !generics.params.is_empty() {
|
2019-12-24 22:38:22 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0567,
|
2019-02-28 22:43:53 +00:00
|
|
|
"auto traits cannot have generic parameters"
|
2019-12-24 22:38:22 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2017-10-15 18:03:03 +00:00
|
|
|
}
|
|
|
|
if !bounds.is_empty() {
|
2019-12-24 22:38:22 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0568,
|
2019-02-28 22:43:53 +00:00
|
|
|
"auto traits cannot have super traits"
|
2019-12-24 22:38:22 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2017-10-15 18:03:03 +00:00
|
|
|
}
|
|
|
|
if !trait_items.is_empty() {
|
2019-12-24 22:38:22 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0380,
|
2019-02-28 22:43:53 +00:00
|
|
|
"auto traits cannot have methods or associated items"
|
2019-12-24 22:38:22 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2017-10-15 18:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 00:33:36 +00:00
|
|
|
self.no_questions_in_bounds(bounds, "supertraits", true);
|
2020-01-05 00:29:45 +00:00
|
|
|
|
|
|
|
// Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
|
|
|
|
// context for the supertraits.
|
|
|
|
self.visit_generics(generics);
|
|
|
|
self.with_bound_context(Some(BoundContext::TraitBounds), |this| {
|
|
|
|
walk_list!(this, visit_param_bound, bounds);
|
|
|
|
});
|
|
|
|
walk_list!(self, visit_trait_item, trait_items);
|
|
|
|
return;
|
2016-08-07 21:33:35 +00:00
|
|
|
}
|
2016-08-12 08:15:40 +00:00
|
|
|
ItemKind::Mod(_) => {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
|
2019-05-08 03:21:18 +00:00
|
|
|
attr::first_attr_value_str_by_name(&item.attrs, sym::path);
|
2016-08-12 08:15:40 +00:00
|
|
|
}
|
2017-12-07 08:52:25 +00:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
2019-03-23 21:06:58 +00:00
|
|
|
if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
|
2019-12-24 22:38:22 +00:00
|
|
|
self.err_handler()
|
|
|
|
.span_err(item.span, "tuple and unit unions are not permitted");
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2018-09-07 13:10:16 +00:00
|
|
|
if vdata.fields().is_empty() {
|
2019-12-24 22:38:22 +00:00
|
|
|
self.err_handler().span_err(item.span, "unions cannot have zero fields");
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
|
2019-09-26 16:58:14 +00:00
|
|
|
match fi.kind {
|
2016-07-16 21:15:15 +00:00
|
|
|
ForeignItemKind::Fn(ref decl, _) => {
|
2019-06-09 10:58:40 +00:00
|
|
|
self.check_fn_decl(decl);
|
2019-10-25 13:15:33 +00:00
|
|
|
Self::check_decl_no_pat(decl, |span, _| {
|
2019-12-24 22:38:22 +00: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-16 21:15:15 +00:00
|
|
|
});
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2018-03-11 02:16:26 +00:00
|
|
|
ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_foreign_item(self, fi)
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Mirrors `visit::walk_generic_args`, but tracks relevant state.
|
2019-01-18 11:35:14 +00:00
|
|
|
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
|
|
|
|
match *generic_args {
|
|
|
|
GenericArgs::AngleBracketed(ref data) => {
|
|
|
|
walk_list!(self, visit_generic_arg, &data.args);
|
2019-03-30 19:57:04 +00:00
|
|
|
validate_generics_order(
|
|
|
|
self.session,
|
|
|
|
self.err_handler(),
|
|
|
|
data.args.iter().map(|arg| {
|
2019-12-24 22:38:22 +00:00
|
|
|
(
|
|
|
|
match arg {
|
|
|
|
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
|
|
|
|
GenericArg::Type(..) => ParamKindOrd::Type,
|
|
|
|
GenericArg::Const(..) => ParamKindOrd::Const,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
arg.span(),
|
|
|
|
None,
|
|
|
|
)
|
2019-03-30 19:57:04 +00:00
|
|
|
}),
|
|
|
|
GenericPosition::Arg,
|
|
|
|
generic_args.span(),
|
|
|
|
);
|
2019-02-20 21:24:32 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
|
2019-03-11 14:14:24 +00:00
|
|
|
// are allowed to contain nested `impl Trait`.
|
|
|
|
self.with_impl_trait(None, |this| {
|
2019-12-24 22:38:22 +00:00
|
|
|
walk_list!(
|
|
|
|
this,
|
|
|
|
visit_assoc_ty_constraint_from_generic_args,
|
|
|
|
&data.constraints
|
|
|
|
);
|
2019-01-18 11:35:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
GenericArgs::Parenthesized(ref data) => {
|
|
|
|
walk_list!(self, visit_ty, &data.inputs);
|
2019-12-01 15:00:08 +00:00
|
|
|
if let FunctionRetTy::Ty(ty) = &data.output {
|
2019-03-11 14:14:24 +00:00
|
|
|
// `-> Foo` syntax is essentially an associated type binding,
|
|
|
|
// so it is also allowed to contain nested `impl Trait`.
|
2019-12-01 15:00:08 +00:00
|
|
|
self.with_impl_trait(None, |this| this.visit_ty(ty));
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:56:01 +00:00
|
|
|
fn visit_generics(&mut self, generics: &'a Generics) {
|
2019-02-05 15:52:17 +00:00
|
|
|
let mut prev_ty_default = None;
|
2018-05-27 15:56:01 +00:00
|
|
|
for param in &generics.params {
|
2019-02-05 15:52:17 +00:00
|
|
|
if let GenericParamKind::Type { ref default, .. } = param.kind {
|
|
|
|
if default.is_some() {
|
|
|
|
prev_ty_default = Some(param.ident.span);
|
|
|
|
} else if let Some(span) = prev_ty_default {
|
2017-10-16 19:07:26 +00:00
|
|
|
self.err_handler()
|
2019-02-05 15:52:17 +00:00
|
|
|
.span_err(span, "type parameters with a default must be trailing");
|
|
|
|
break;
|
2017-10-16 19:07:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2019-03-30 19:57:04 +00:00
|
|
|
validate_generics_order(
|
|
|
|
self.session,
|
|
|
|
self.err_handler(),
|
|
|
|
generics.params.iter().map(|param| {
|
|
|
|
let ident = Some(param.ident.to_string());
|
|
|
|
let (kind, ident) = match ¶m.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
|
|
|
|
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
|
|
|
|
GenericParamKind::Const { ref ty } => {
|
|
|
|
let ty = pprust::ty_to_string(ty);
|
|
|
|
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(kind, Some(&*param.bounds), param.ident.span, ident)
|
|
|
|
}),
|
|
|
|
GenericPosition::Param,
|
|
|
|
generics.span,
|
|
|
|
);
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2018-05-27 15:56:01 +00:00
|
|
|
for predicate in &generics.where_clause.predicates {
|
2017-01-17 18:18:29 +00:00
|
|
|
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
|
2019-01-08 23:53:43 +00:00
|
|
|
self.err_handler()
|
2019-12-22 18:18:49 +00:00
|
|
|
.struct_span_err(
|
|
|
|
predicate.span,
|
|
|
|
"equality constraints are not yet supported in `where` clauses",
|
|
|
|
)
|
2019-12-13 05:15:19 +00:00
|
|
|
.span_label(predicate.span, "not supported")
|
2019-12-22 18:18:49 +00:00
|
|
|
.note(
|
|
|
|
"for more information, see https://github.com/rust-lang/rust/issues/20041",
|
|
|
|
)
|
|
|
|
.emit();
|
2017-01-17 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2018-05-27 15:56:01 +00:00
|
|
|
visit::walk_generics(self, generics)
|
2017-01-17 18:18:29 +00:00
|
|
|
}
|
2017-06-24 18:26:04 +00:00
|
|
|
|
2018-05-30 11:36:53 +00:00
|
|
|
fn visit_generic_param(&mut self, param: &'a GenericParam) {
|
2018-05-31 14:52:17 +00:00
|
|
|
if let GenericParamKind::Lifetime { .. } = param.kind {
|
|
|
|
self.check_lifetime(param.ident);
|
2018-05-30 11:36:53 +00:00
|
|
|
}
|
|
|
|
visit::walk_generic_param(self, param);
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:29:45 +00:00
|
|
|
fn visit_param_bound(&mut self, bound: &'a GenericBound) {
|
|
|
|
if let GenericBound::Trait(poly, maybe_bound) = bound {
|
|
|
|
match poly.trait_ref.constness {
|
|
|
|
Some(Constness::NotConst) => {
|
|
|
|
if *maybe_bound == TraitBoundModifier::Maybe {
|
|
|
|
self.err_handler()
|
|
|
|
.span_err(bound.span(), "`?const` and `?` are mutually exclusive");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ctx) = self.innermost_bound_context() {
|
|
|
|
let msg = format!("`?const` is not permitted in {}", ctx.description());
|
|
|
|
self.err_handler().span_err(bound.span(), &msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Constness::Const) => bug!("Parser should reject bare `const` on bounds"),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_param_bound(self, bound)
|
|
|
|
}
|
|
|
|
|
2017-06-24 18:26:04 +00:00
|
|
|
fn visit_pat(&mut self, pat: &'a Pat) {
|
2019-09-26 15:18:31 +00:00
|
|
|
match pat.kind {
|
2017-06-24 18:26:04 +00:00
|
|
|
PatKind::Lit(ref expr) => {
|
2017-08-13 13:59:54 +00: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 13:59:54 +00: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 10:22:24 +00: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-11 02:16:26 +00:00
|
|
|
|
2019-08-24 16:54:40 +00:00
|
|
|
fn visit_variant_data(&mut self, s: &'a VariantData) {
|
2019-03-21 17:55:09 +00:00
|
|
|
self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
|
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
fn visit_enum_def(
|
|
|
|
&mut self,
|
|
|
|
enum_definition: &'a EnumDef,
|
|
|
|
generics: &'a Generics,
|
|
|
|
item_id: NodeId,
|
|
|
|
_: Span,
|
|
|
|
) {
|
|
|
|
self.with_banned_assoc_ty_bound(|this| {
|
|
|
|
visit::walk_enum_def(this, enum_definition, generics, item_id)
|
|
|
|
})
|
2019-03-21 17:55:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 16:11:07 +00:00
|
|
|
fn visit_impl_item(&mut self, ii: &'a AssocItem) {
|
2019-12-01 03:12:28 +00:00
|
|
|
match &ii.kind {
|
2019-12-01 16:11:07 +00:00
|
|
|
AssocItemKind::Const(_, body) => {
|
2019-12-01 06:24:07 +00:00
|
|
|
self.check_impl_item_provided(ii.span, body, "constant", " = <expr>;");
|
2019-12-01 03:12:28 +00:00
|
|
|
}
|
2019-12-07 23:13:59 +00:00
|
|
|
AssocItemKind::Fn(sig, body) => {
|
2019-12-01 06:24:07 +00:00
|
|
|
self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
|
2019-12-01 03:12:28 +00:00
|
|
|
self.check_fn_decl(&sig.decl);
|
|
|
|
}
|
2019-12-01 16:11:07 +00:00
|
|
|
AssocItemKind::TyAlias(bounds, body) => {
|
2019-12-01 09:25:45 +00:00
|
|
|
self.check_impl_item_provided(ii.span, body, "type", " = <type>;");
|
|
|
|
self.check_impl_assoc_type_no_bounds(bounds);
|
|
|
|
}
|
2019-12-01 03:12:28 +00:00
|
|
|
_ => {}
|
2019-06-09 10:58:40 +00:00
|
|
|
}
|
2019-12-02 01:03:31 +00:00
|
|
|
visit::walk_impl_item(self, ii);
|
2019-06-09 10:58:40 +00:00
|
|
|
}
|
2019-11-07 10:26:36 +00:00
|
|
|
|
2019-12-01 16:11:07 +00:00
|
|
|
fn visit_trait_item(&mut self, ti: &'a AssocItem) {
|
2019-11-07 10:26:36 +00:00
|
|
|
self.invalid_visibility(&ti.vis, None);
|
2019-11-30 17:25:44 +00:00
|
|
|
self.check_defaultness(ti.span, ti.defaultness);
|
2019-12-02 01:53:18 +00:00
|
|
|
|
2019-12-07 23:13:59 +00:00
|
|
|
if let AssocItemKind::Fn(sig, block) = &ti.kind {
|
2019-12-02 01:53:18 +00:00
|
|
|
self.check_fn_decl(&sig.decl);
|
|
|
|
self.check_trait_fn_not_async(ti.span, sig.header.asyncness.node);
|
|
|
|
self.check_trait_fn_not_const(sig.header.constness);
|
|
|
|
if block.is_none() {
|
|
|
|
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
|
|
|
|
if mut_ident {
|
|
|
|
self.lint_buffer.buffer_lint(
|
|
|
|
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
|
2019-12-24 22:38:22 +00:00
|
|
|
ti.id,
|
|
|
|
span,
|
|
|
|
"patterns aren't allowed in methods without bodies",
|
2019-12-02 01:53:18 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
struct_span_err!(
|
2019-12-24 22:38:22 +00:00
|
|
|
self.session,
|
|
|
|
span,
|
|
|
|
E0642,
|
2019-12-02 01:53:18 +00:00
|
|
|
"patterns aren't allowed in methods without bodies"
|
2019-12-24 22:38:22 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2019-12-02 01:53:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 01:03:31 +00:00
|
|
|
visit::walk_trait_item(self, ti);
|
2019-11-07 10:26:36 +00:00
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
|
|
|
|
fn visit_assoc_item(&mut self, item: &'a AssocItem) {
|
2019-12-07 23:13:59 +00:00
|
|
|
if let AssocItemKind::Fn(sig, _) = &item.kind {
|
2019-12-02 01:38:33 +00:00
|
|
|
self.check_c_varadic_type(&sig.decl);
|
|
|
|
}
|
|
|
|
visit::walk_assoc_item(self, item);
|
|
|
|
}
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:15:33 +00:00
|
|
|
pub fn check_crate(session: &Session, krate: &Crate, lints: &mut lint::LintBuffer) -> bool {
|
2019-01-17 06:28:39 +00:00
|
|
|
let mut validator = AstValidator {
|
2019-01-18 11:35:14 +00:00
|
|
|
session,
|
2019-01-17 06:28:39 +00:00
|
|
|
has_proc_macro_decls: false,
|
2019-01-18 11:35:14 +00:00
|
|
|
outer_impl_trait: None,
|
2020-01-05 00:29:45 +00:00
|
|
|
bound_context_stack: Vec::new(),
|
2019-01-18 11:35:14 +00:00
|
|
|
is_impl_trait_banned: false,
|
2019-03-21 17:55:09 +00:00
|
|
|
is_assoc_ty_bound_banned: false,
|
2019-10-25 13:15:33 +00:00
|
|
|
lint_buffer: lints,
|
2019-01-17 06:28:39 +00:00
|
|
|
};
|
|
|
|
visit::walk_crate(&mut validator, krate);
|
|
|
|
|
2019-07-18 21:24:58 +00:00
|
|
|
validator.has_proc_macro_decls
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|