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.
|
|
|
|
|
2020-04-05 23:34:16 +00:00
|
|
|
use itertools::{Either, Itertools};
|
2020-03-11 16:17:55 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
|
|
|
|
use rustc_ast::walk_list;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::*;
|
2020-01-11 16:02:46 +00:00
|
|
|
use rustc_ast_pretty::pprust;
|
2019-02-05 15:52:17 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-03-28 20:48:04 +00:00
|
|
|
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
|
2019-10-15 20:48:13 +00:00
|
|
|
use rustc_parse::validate_attr;
|
2021-07-08 19:58:05 +00:00
|
|
|
use rustc_session::lint::builtin::{MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY};
|
2020-11-30 14:24:08 +00:00
|
|
|
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
|
2021-08-25 13:38:14 +00:00
|
|
|
use rustc_session::Session;
|
2021-08-08 14:49:13 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2021-07-08 19:58:05 +00:00
|
|
|
use rustc_target::spec::abi;
|
2019-12-24 22:38:22 +00:00
|
|
|
use std::mem;
|
2020-04-08 18:10:16 +00:00
|
|
|
use std::ops::DerefMut;
|
2016-05-22 14:51:22 +00:00
|
|
|
|
2020-02-14 11:55:42 +00:00
|
|
|
const MORE_EXTERN: &str =
|
|
|
|
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
|
|
|
|
|
2020-01-29 00:30:01 +00:00
|
|
|
/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
|
|
|
|
enum SelfSemantic {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
struct AstValidator<'a> {
|
2016-05-22 14:51:22 +00:00
|
|
|
session: &'a Session,
|
2020-01-29 23:18:54 +00:00
|
|
|
|
|
|
|
/// The span of the `extern` in an `extern { ... }` block, if any.
|
|
|
|
extern_mod: Option<&'a Item>,
|
|
|
|
|
|
|
|
/// Are we inside a trait impl?
|
|
|
|
in_trait_impl: bool,
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
in_const_trait_impl: bool,
|
|
|
|
|
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
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
is_tilde_const_allowed: bool,
|
2020-01-05 00:29:45 +00:00
|
|
|
|
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,
|
|
|
|
|
2021-08-08 14:49:13 +00:00
|
|
|
/// Used to allow `let` expressions in certain syntactic locations.
|
|
|
|
is_let_allowed: bool,
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
lint_buffer: &'a mut LintBuffer,
|
2019-05-15 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
impl<'a> AstValidator<'a> {
|
2021-08-25 11:53:16 +00:00
|
|
|
fn with_in_trait_impl(
|
|
|
|
&mut self,
|
|
|
|
is_in: bool,
|
|
|
|
constness: Option<Const>,
|
|
|
|
f: impl FnOnce(&mut Self),
|
|
|
|
) {
|
2020-02-05 11:27:45 +00:00
|
|
|
let old = mem::replace(&mut self.in_trait_impl, is_in);
|
2021-08-25 11:53:16 +00:00
|
|
|
let old_const =
|
|
|
|
mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_))));
|
2020-02-05 11:27:45 +00:00
|
|
|
f(self);
|
|
|
|
self.in_trait_impl = old;
|
2021-08-25 11:53:16 +00:00
|
|
|
self.in_const_trait_impl = old_const;
|
2020-02-05 11:27:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
|
|
|
|
let old = mem::replace(&mut self.is_tilde_const_allowed, true);
|
|
|
|
f(self);
|
|
|
|
self.is_tilde_const_allowed = old;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_banned_tilde_const(&mut self, f: impl FnOnce(&mut Self)) {
|
|
|
|
let old = mem::replace(&mut self.is_tilde_const_allowed, false);
|
|
|
|
f(self);
|
|
|
|
self.is_tilde_const_allowed = old;
|
|
|
|
}
|
|
|
|
|
2021-08-08 14:49:13 +00:00
|
|
|
fn with_let_allowed(&mut self, allowed: bool, f: impl FnOnce(&mut Self, bool)) {
|
|
|
|
let old = mem::replace(&mut self.is_let_allowed, allowed);
|
|
|
|
f(self, old);
|
|
|
|
self.is_let_allowed = old;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits an error banning the `let` expression provided in the given location.
|
|
|
|
fn ban_let_expr(&self, expr: &'a Expr) {
|
|
|
|
let sess = &self.session;
|
|
|
|
if sess.opts.unstable_features.is_nightly_build() {
|
|
|
|
sess.struct_span_err(expr.span, "`let` expressions are not supported here")
|
|
|
|
.note("only supported directly in conditions of `if`- and `while`-expressions")
|
|
|
|
.note("as well as when nested within `&&` and parenthesis in those conditions")
|
|
|
|
.emit();
|
|
|
|
} else {
|
|
|
|
sess.struct_span_err(expr.span, "expected expression, found statement (`let`)")
|
|
|
|
.note("variable declaration using `let` is a statement")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2019-06-22 23:39:13 +00:00
|
|
|
let old = mem::replace(&mut self.outer_impl_trait, outer);
|
2020-01-06 00:33:26 +00:00
|
|
|
if outer.is_some() {
|
2021-08-25 11:53:16 +00:00
|
|
|
self.with_banned_tilde_const(f);
|
2020-01-06 00:33:26 +00:00
|
|
|
} else {
|
2021-08-25 11:53:16 +00:00
|
|
|
f(self);
|
2020-01-06 00:33:26 +00:00
|
|
|
}
|
2019-06-22 23:39:13 +00:00
|
|
|
self.outer_impl_trait = old;
|
2020-01-05 00:29:45 +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
|
|
|
}
|
2021-08-25 11:53:16 +00:00
|
|
|
TyKind::TraitObject(..) => self.with_banned_tilde_const(|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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:51:00 +00:00
|
|
|
fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
|
|
|
|
if let Some(ident) = field.ident {
|
|
|
|
if ident.name == kw::Underscore {
|
|
|
|
self.visit_vis(&field.vis);
|
|
|
|
self.visit_ident(ident);
|
|
|
|
self.visit_ty_common(&field.ty);
|
|
|
|
self.walk_ty(&field.ty);
|
|
|
|
walk_list!(self, visit_attribute, &field.attrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.visit_field_def(field);
|
|
|
|
}
|
|
|
|
|
2020-01-09 10:18:47 +00:00
|
|
|
fn err_handler(&self) -> &rustc_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) {
|
2020-12-30 01:28:08 +00:00
|
|
|
let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Empty];
|
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>) {
|
2020-08-21 23:11:00 +00:00
|
|
|
if let VisibilityKind::Inherited = vis.kind {
|
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");
|
2020-08-21 23:11:00 +00:00
|
|
|
if vis.kind.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
|
|
|
|
2020-11-30 14:24:08 +00:00
|
|
|
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
|
2019-12-01 11:43:39 +00:00
|
|
|
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 => {}
|
2020-11-30 14:24:08 +00:00
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => {
|
|
|
|
report_err(pat.span, Some(ident), true)
|
2019-12-24 22:38:22 +00:00
|
|
|
}
|
2020-11-30 14:24:08 +00:00
|
|
|
_ => report_err(pat.span, None, false),
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 21:33:35 +00:00
|
|
|
|
2020-01-30 04:31:04 +00:00
|
|
|
fn check_trait_fn_not_async(&self, fn_span: Span, asyncness: Async) {
|
|
|
|
if let Async::Yes { span, .. } = asyncness {
|
2020-02-11 07:19:21 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
fn_span,
|
|
|
|
E0706,
|
|
|
|
"functions in traits cannot be declared `async`"
|
|
|
|
)
|
|
|
|
.span_label(span, "`async` because of this")
|
|
|
|
.note("`async` trait functions are not currently supported")
|
|
|
|
.note("consider using the `async-trait` crate: https://crates.io/crates/async-trait")
|
|
|
|
.emit();
|
2018-06-19 04:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:42:33 +00:00
|
|
|
fn check_trait_fn_not_const(&self, constness: Const) {
|
|
|
|
if let Const::Yes(span) = constness {
|
2020-02-11 07:19:21 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
span,
|
|
|
|
E0379,
|
|
|
|
"functions in traits cannot be declared const"
|
|
|
|
)
|
|
|
|
.span_label(span, "functions in traits cannot be const")
|
|
|
|
.emit();
|
2016-08-07 21:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 00:33:36 +00:00
|
|
|
|
2020-01-06 00:33:26 +00:00
|
|
|
// FIXME(ecstaticmorse): Instead, use `bound_context` 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 {
|
2020-10-06 19:58:53 +00:00
|
|
|
ExprKind::Lit(..) | ExprKind::ConstBlock(..) | ExprKind::Err => {}
|
2017-08-13 13:59:54 +00:00
|
|
|
ExprKind::Path(..) if allow_paths => {}
|
2020-03-26 09:52:52 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
|
2019-12-24 22:38:22 +00:00
|
|
|
_ => 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
|
|
|
|
2020-01-29 00:30:01 +00:00
|
|
|
fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
|
2021-09-07 21:48:03 +00:00
|
|
|
self.check_decl_num_args(fn_decl);
|
2020-01-29 00:30:01 +00:00
|
|
|
self.check_decl_cvaradic_pos(fn_decl);
|
|
|
|
self.check_decl_attrs(fn_decl);
|
|
|
|
self.check_decl_self_param(fn_decl, self_semantic);
|
|
|
|
}
|
|
|
|
|
2021-09-07 21:48:03 +00:00
|
|
|
/// Emits fatal error if function declaration has more than `u16::MAX` arguments
|
|
|
|
/// Error is fatal to prevent errors during typechecking
|
|
|
|
fn check_decl_num_args(&self, fn_decl: &FnDecl) {
|
|
|
|
let max_num_args: usize = u16::MAX.into();
|
|
|
|
if fn_decl.inputs.len() > max_num_args {
|
|
|
|
let Param { span, .. } = fn_decl.inputs[0];
|
|
|
|
self.err_handler().span_fatal(
|
|
|
|
span,
|
|
|
|
&format!("function can not have more than {} arguments", max_num_args),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 00:30:01 +00:00
|
|
|
fn check_decl_cvaradic_pos(&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
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-01-29 00:30:01 +00:00
|
|
|
}
|
2019-12-02 02:16:12 +00:00
|
|
|
|
2020-01-29 00:30:01 +00:00
|
|
|
fn check_decl_attrs(&self, fn_decl: &FnDecl) {
|
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];
|
2020-01-11 12:15:20 +00:00
|
|
|
!arr.contains(&attr.name_or_empty()) && rustc_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
|
|
|
|
2020-01-29 00:30:01 +00:00
|
|
|
fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
|
|
|
|
if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) {
|
|
|
|
if param.is_self() {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
param.span,
|
2020-02-02 10:10:27 +00:00
|
|
|
"`self` parameter is only allowed in associated functions",
|
2020-01-29 00:30:01 +00:00
|
|
|
)
|
|
|
|
.span_label(param.span, "not semantically valid as function parameter")
|
2020-02-02 10:10:27 +00:00
|
|
|
.note("associated functions are those in `impl` or `trait` definitions")
|
2020-01-29 00:30:01 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:25:44 +00:00
|
|
|
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
|
2020-02-22 01:01:53 +00:00
|
|
|
if let Defaultness::Default(def_span) = defaultness {
|
2020-03-09 18:42:37 +00:00
|
|
|
let span = self.session.source_map().guess_head_span(span);
|
2019-11-30 17:25:44 +00:00
|
|
|
self.err_handler()
|
2020-12-09 05:56:22 +00:00
|
|
|
.struct_span_err(span, "`default` is only allowed on items in trait impls")
|
2020-02-22 01:01:53 +00:00
|
|
|
.span_label(def_span, "`default` because of this")
|
2019-11-30 17:25:44 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 06:24:07 +00:00
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn error_item_without_body(&self, sp: Span, ctx: &str, msg: &str, sugg: &str) {
|
2019-12-01 06:24:07 +00:00
|
|
|
self.err_handler()
|
2020-01-29 23:18:54 +00:00
|
|
|
.struct_span_err(sp, msg)
|
2019-12-01 06:24:07 +00:00
|
|
|
.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
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) {
|
|
|
|
if body.is_none() {
|
|
|
|
let msg = format!("associated {} in `impl` without body", ctx);
|
|
|
|
self.error_item_without_body(sp, ctx, &msg, sugg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:55:42 +00:00
|
|
|
fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) {
|
2019-12-01 09:25:45 +00:00
|
|
|
let span = match bounds {
|
|
|
|
[] => return,
|
|
|
|
[b0] => b0.span(),
|
|
|
|
[b0, .., bl] => b0.span().to(bl.span()),
|
|
|
|
};
|
|
|
|
self.err_handler()
|
2020-02-14 11:55:42 +00:00
|
|
|
.struct_span_err(span, &format!("bounds on `type`s in {} have no effect", ctx))
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_foreign_ty_genericless(&self, generics: &Generics) {
|
|
|
|
let cannot_have = |span, descr, remove_descr| {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
span,
|
|
|
|
&format!("`type`s inside `extern` blocks cannot have {}", descr),
|
|
|
|
)
|
|
|
|
.span_suggestion(
|
|
|
|
span,
|
|
|
|
&format!("remove the {}", remove_descr),
|
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.span_label(self.current_extern_span(), "`extern` block begins here")
|
|
|
|
.note(MORE_EXTERN)
|
|
|
|
.emit();
|
|
|
|
};
|
|
|
|
|
|
|
|
if !generics.params.is_empty() {
|
|
|
|
cannot_have(generics.span, "generic parameters", "generic parameters");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !generics.where_clause.predicates.is_empty() {
|
|
|
|
cannot_have(generics.where_clause.span, "`where` clauses", "`where` clause");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:56:05 +00:00
|
|
|
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
|
2020-02-14 11:55:42 +00:00
|
|
|
let body = match body {
|
|
|
|
None => return,
|
|
|
|
Some(body) => body,
|
|
|
|
};
|
|
|
|
self.err_handler()
|
2020-02-14 14:56:05 +00:00
|
|
|
.struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind))
|
2020-02-14 11:55:42 +00:00
|
|
|
.span_label(ident.span, "cannot have a body")
|
2020-02-14 14:56:05 +00:00
|
|
|
.span_label(body, "the invalid body")
|
2020-02-14 11:55:42 +00:00
|
|
|
.span_label(
|
|
|
|
self.current_extern_span(),
|
2020-02-14 14:56:05 +00:00
|
|
|
format!(
|
|
|
|
"`extern` blocks define existing foreign {0}s and {0}s \
|
|
|
|
inside of them cannot have a body",
|
|
|
|
kind
|
|
|
|
),
|
2020-02-14 11:55:42 +00:00
|
|
|
)
|
|
|
|
.note(MORE_EXTERN)
|
2019-12-01 09:25:45 +00:00
|
|
|
.emit();
|
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
/// An `fn` in `extern { ... }` cannot have a body `{ ... }`.
|
|
|
|
fn check_foreign_fn_bodyless(&self, ident: Ident, body: Option<&Block>) {
|
|
|
|
let body = match body {
|
|
|
|
None => return,
|
|
|
|
Some(body) => body,
|
|
|
|
};
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(ident.span, "incorrect function inside `extern` block")
|
|
|
|
.span_label(ident.span, "cannot have a body")
|
|
|
|
.span_suggestion(
|
|
|
|
body.span,
|
|
|
|
"remove the invalid body",
|
|
|
|
";".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.help(
|
|
|
|
"you might have meant to write a function accessible through FFI, \
|
|
|
|
which can be done by writing `extern fn` outside of the `extern` block",
|
|
|
|
)
|
|
|
|
.span_label(
|
|
|
|
self.current_extern_span(),
|
|
|
|
"`extern` blocks define existing foreign functions and functions \
|
|
|
|
inside of them cannot have a body",
|
|
|
|
)
|
2020-02-14 11:55:42 +00:00
|
|
|
.note(MORE_EXTERN)
|
2020-01-29 23:18:54 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_extern_span(&self) -> Span {
|
2020-03-09 18:42:37 +00:00
|
|
|
self.session.source_map().guess_head_span(self.extern_mod.unwrap().span)
|
2020-01-29 23:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 15:51:46 +00:00
|
|
|
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
|
2020-01-29 23:18:54 +00:00
|
|
|
fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) {
|
|
|
|
if header.has_qualifiers() {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers")
|
|
|
|
.span_label(self.current_extern_span(), "in this `extern` block")
|
2020-11-15 20:36:07 +00:00
|
|
|
.span_suggestion_verbose(
|
2020-01-29 23:18:54 +00:00
|
|
|
span.until(ident.span.shrink_to_lo()),
|
|
|
|
"remove the qualifiers",
|
|
|
|
"fn ".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:49:59 +00:00
|
|
|
/// An item in `extern { ... }` cannot use non-ascii identifier.
|
|
|
|
fn check_foreign_item_ascii_only(&self, ident: Ident) {
|
|
|
|
let symbol_str = ident.as_str();
|
|
|
|
if !symbol_str.is_ascii() {
|
|
|
|
let n = 83942;
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
ident.span,
|
|
|
|
"items in `extern` blocks cannot use non-ascii identifiers",
|
|
|
|
)
|
|
|
|
.span_label(self.current_extern_span(), "in this `extern` block")
|
|
|
|
.note(&format!(
|
2021-10-03 06:53:02 +00:00
|
|
|
"this limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
|
2021-04-06 15:49:59 +00:00
|
|
|
n, n,
|
|
|
|
))
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
/// Reject C-varadic type unless the function is foreign,
|
|
|
|
/// or free and `unsafe extern "C"` semantically.
|
|
|
|
fn check_c_varadic_type(&self, fk: FnKind<'a>) {
|
|
|
|
match (fk.ctxt(), fk.header()) {
|
|
|
|
(Some(FnCtxt::Foreign), _) => return,
|
|
|
|
(Some(FnCtxt::Free), Some(header)) => match header.ext {
|
|
|
|
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }) | Extern::Implicit
|
2020-01-30 01:42:33 +00:00
|
|
|
if matches!(header.unsafety, Unsafe::Yes(_)) =>
|
2020-01-29 23:18:54 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
for Param { ty, span, .. } in &fk.decl().inputs {
|
2019-12-02 01:38:33 +00:00
|
|
|
if let TyKind::CVarArgs = ty.kind {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
*span,
|
2021-06-09 10:41:03 +00:00
|
|
|
"only foreign or `unsafe extern \"C\"` functions may be C-variadic",
|
2019-12-02 01:38:33 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
|
2020-02-14 14:56:05 +00:00
|
|
|
fn check_item_named(&self, ident: Ident, kind: &str) {
|
|
|
|
if ident.name != kw::Underscore {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind))
|
|
|
|
.span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
|
|
|
|
.emit();
|
|
|
|
}
|
2020-03-11 16:17:55 +00:00
|
|
|
|
2020-05-16 06:09:01 +00:00
|
|
|
fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
|
|
|
|
if ident.name.as_str().is_ascii() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let head_span = self.session.source_map().guess_head_span(item_span);
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
head_span,
|
|
|
|
E0754,
|
|
|
|
"`#[no_mangle]` requires ASCII identifier"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_mod_file_item_asciionly(&self, ident: Ident) {
|
|
|
|
if ident.name.as_str().is_ascii() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
ident.span,
|
|
|
|
E0754,
|
2021-04-06 15:49:59 +00:00
|
|
|
"trying to load file for module `{}` with non-ascii identifier name",
|
2020-05-16 06:09:01 +00:00
|
|
|
ident.name
|
|
|
|
)
|
|
|
|
.help("consider using `#[path]` attribute to specify filesystem path")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:17:55 +00:00
|
|
|
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
|
|
|
|
if !generics.params.is_empty() {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
generics.span,
|
|
|
|
E0567,
|
|
|
|
"auto traits cannot have generic parameters"
|
|
|
|
)
|
|
|
|
.span_label(ident_span, "auto trait cannot have generic parameters")
|
|
|
|
.span_suggestion(
|
|
|
|
generics.span,
|
|
|
|
"remove the parameters",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 15:58:10 +00:00
|
|
|
fn emit_e0568(&self, span: Span, ident_span: Span) {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
span,
|
|
|
|
E0568,
|
|
|
|
"auto traits cannot have super traits or lifetime bounds"
|
|
|
|
)
|
|
|
|
.span_label(ident_span, "auto trait cannot have super traits or lifetime bounds")
|
|
|
|
.span_suggestion(
|
|
|
|
span,
|
|
|
|
"remove the super traits or lifetime bounds",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:17:55 +00:00
|
|
|
fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
|
2021-10-03 15:58:10 +00:00
|
|
|
if let [.., last] = &bounds[..] {
|
|
|
|
let span = ident_span.shrink_to_hi().to(last.span());
|
|
|
|
self.emit_e0568(span, ident_span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
|
|
|
|
if !where_clause.predicates.is_empty() {
|
|
|
|
self.emit_e0568(where_clause.span, ident_span);
|
2020-03-11 16:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
|
|
|
|
if !trait_items.is_empty() {
|
|
|
|
let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
|
2021-10-03 15:58:10 +00:00
|
|
|
let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
|
2020-03-11 16:17:55 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
spans,
|
|
|
|
E0380,
|
2021-10-03 15:58:10 +00:00
|
|
|
"auto traits cannot have associated items"
|
|
|
|
)
|
|
|
|
.span_suggestion(
|
|
|
|
total_span,
|
|
|
|
"remove these associated items",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
2020-03-11 16:17:55 +00:00
|
|
|
)
|
2021-10-03 15:58:10 +00:00
|
|
|
.span_label(ident_span, "auto trait cannot have associated items")
|
2020-03-11 16:17:55 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 03:40:05 +00:00
|
|
|
|
2020-04-05 23:34:16 +00:00
|
|
|
fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String {
|
2020-03-29 18:31:58 +00:00
|
|
|
// Lifetimes always come first.
|
|
|
|
let lt_sugg = data.args.iter().filter_map(|arg| match arg {
|
|
|
|
AngleBracketedArg::Arg(lt @ GenericArg::Lifetime(_)) => {
|
|
|
|
Some(pprust::to_string(|s| s.print_generic_arg(lt)))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let args_sugg = data.args.iter().filter_map(|a| match a {
|
2020-04-05 23:34:16 +00:00
|
|
|
AngleBracketedArg::Arg(GenericArg::Lifetime(_)) | AngleBracketedArg::Constraint(_) => {
|
|
|
|
None
|
|
|
|
}
|
2020-03-29 18:31:58 +00:00
|
|
|
AngleBracketedArg::Arg(arg) => Some(pprust::to_string(|s| s.print_generic_arg(arg))),
|
|
|
|
});
|
2020-04-05 23:34:16 +00:00
|
|
|
// Constraints always come last.
|
2020-03-29 18:31:58 +00:00
|
|
|
let constraint_sugg = data.args.iter().filter_map(|a| match a {
|
|
|
|
AngleBracketedArg::Arg(_) => None,
|
|
|
|
AngleBracketedArg::Constraint(c) => {
|
|
|
|
Some(pprust::to_string(|s| s.print_assoc_constraint(c)))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
format!(
|
|
|
|
"<{}>",
|
|
|
|
lt_sugg.chain(args_sugg).chain(constraint_sugg).collect::<Vec<String>>().join(", ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-22 03:40:05 +00:00
|
|
|
/// Enforce generic args coming before constraints in `<...>` of a path segment.
|
|
|
|
fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) {
|
|
|
|
// Early exit in case it's partitioned as it should be.
|
|
|
|
if data.args.iter().is_partitioned(|arg| matches!(arg, AngleBracketedArg::Arg(_))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Find all generic argument coming after the first constraint...
|
2020-04-05 23:34:16 +00:00
|
|
|
let (constraint_spans, arg_spans): (Vec<Span>, Vec<Span>) =
|
|
|
|
data.args.iter().partition_map(|arg| match arg {
|
|
|
|
AngleBracketedArg::Constraint(c) => Either::Left(c.span),
|
|
|
|
AngleBracketedArg::Arg(a) => Either::Right(a.span()),
|
|
|
|
});
|
2020-03-29 18:31:58 +00:00
|
|
|
let args_len = arg_spans.len();
|
2020-03-28 20:48:04 +00:00
|
|
|
let constraint_len = constraint_spans.len();
|
2020-03-22 03:40:05 +00:00
|
|
|
// ...and then error:
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(
|
2020-03-28 20:48:04 +00:00
|
|
|
arg_spans.clone(),
|
2020-03-27 06:39:10 +00:00
|
|
|
"generic arguments must come before the first constraint",
|
2020-03-22 03:40:05 +00:00
|
|
|
)
|
2020-04-06 00:02:44 +00:00
|
|
|
.span_label(constraint_spans[0], &format!("constraint{}", pluralize!(constraint_len)))
|
|
|
|
.span_label(
|
|
|
|
*arg_spans.iter().last().unwrap(),
|
|
|
|
&format!("generic argument{}", pluralize!(args_len)),
|
|
|
|
)
|
|
|
|
.span_labels(constraint_spans, "")
|
|
|
|
.span_labels(arg_spans, "")
|
2020-03-29 18:31:58 +00:00
|
|
|
.span_suggestion_verbose(
|
|
|
|
data.span,
|
|
|
|
&format!(
|
|
|
|
"move the constraint{} after the generic argument{}",
|
|
|
|
pluralize!(constraint_len),
|
|
|
|
pluralize!(args_len)
|
|
|
|
),
|
2020-04-05 23:34:16 +00:00
|
|
|
self.correct_generic_order_suggestion(&data),
|
2020-03-29 00:45:36 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
2020-03-22 03:40:05 +00:00
|
|
|
.emit();
|
|
|
|
}
|
2021-05-16 14:51:00 +00:00
|
|
|
|
|
|
|
fn visit_ty_common(&mut self, ty: &'a Ty) {
|
|
|
|
match ty.kind {
|
|
|
|
TyKind::BareFn(ref bfty) => {
|
|
|
|
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
|
|
|
|
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
span,
|
|
|
|
E0561,
|
|
|
|
"patterns aren't allowed in function pointer types"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
self.check_late_bound_lifetime_defs(&bfty.generic_params);
|
2021-07-08 19:58:05 +00:00
|
|
|
if let Extern::Implicit = bfty.ext {
|
|
|
|
let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo());
|
|
|
|
self.maybe_lint_missing_abi(sig_span, ty.id);
|
|
|
|
}
|
2021-05-16 14:51:00 +00:00
|
|
|
}
|
|
|
|
TyKind::TraitObject(ref bounds, ..) => {
|
|
|
|
let mut any_lifetime_bounds = false;
|
|
|
|
for bound in bounds {
|
|
|
|
if let GenericBound::Outlives(ref lifetime) = *bound {
|
|
|
|
if any_lifetime_bounds {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
lifetime.ident.span,
|
|
|
|
E0226,
|
|
|
|
"only a single explicit lifetime bound is permitted"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
any_lifetime_bounds = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.no_questions_in_bounds(bounds, "trait object types", false);
|
|
|
|
}
|
|
|
|
TyKind::ImplTrait(_, ref bounds) => {
|
|
|
|
if self.is_impl_trait_banned {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
ty.span,
|
|
|
|
E0667,
|
|
|
|
"`impl Trait` is not allowed in path parameters"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
ty.span,
|
|
|
|
E0666,
|
|
|
|
"nested `impl Trait` is not allowed"
|
|
|
|
)
|
|
|
|
.span_label(outer_impl_trait_sp, "outer `impl Trait`")
|
|
|
|
.span_label(ty.span, "nested `impl Trait` here")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) {
|
|
|
|
self.err_handler().span_err(ty.span, "at least one trait must be specified");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 19:58:05 +00:00
|
|
|
|
|
|
|
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
|
|
|
|
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
|
|
|
|
// call site which do not have a macro backtrace. See #61963.
|
|
|
|
let is_macro_callsite = self
|
|
|
|
.session
|
|
|
|
.source_map()
|
|
|
|
.span_to_snippet(span)
|
|
|
|
.map(|snippet| snippet.starts_with("#["))
|
|
|
|
.unwrap_or(true);
|
|
|
|
if !is_macro_callsite {
|
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
|
|
|
MISSING_ABI,
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
"extern declarations without an explicit ABI are deprecated",
|
|
|
|
BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 19:20:09 +00:00
|
|
|
/// Checks that generic parameters are in the correct order,
|
|
|
|
/// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
|
2020-12-30 16:43:30 +00:00
|
|
|
fn validate_generic_param_order(
|
2019-03-30 19:57:04 +00:00
|
|
|
sess: &Session,
|
2020-01-09 10:18:47 +00:00
|
|
|
handler: &rustc_errors::Handler,
|
2020-12-30 16:43:30 +00:00
|
|
|
generics: &[GenericParam],
|
2019-02-05 15:52:17 +00:00
|
|
|
span: Span,
|
|
|
|
) {
|
|
|
|
let mut max_param: Option<ParamKindOrd> = None;
|
|
|
|
let mut out_of_order = FxHashMap::default();
|
2021-06-09 21:09:12 +00:00
|
|
|
let mut param_idents = Vec::with_capacity(generics.len());
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2021-06-09 21:09:12 +00:00
|
|
|
for (idx, param) in generics.iter().enumerate() {
|
|
|
|
let ident = param.ident;
|
|
|
|
let (kind, bounds, span) = (¶m.kind, ¶m.bounds, ident.span);
|
2020-12-30 16:43:30 +00:00
|
|
|
let (ord_kind, ident) = match ¶m.kind {
|
2021-06-09 21:09:12 +00:00
|
|
|
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
|
|
|
|
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
|
2020-12-31 00:58:27 +00:00
|
|
|
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
|
2020-12-30 16:43:30 +00:00
|
|
|
let ty = pprust::ty_to_string(ty);
|
2021-04-18 12:31:00 +00:00
|
|
|
let unordered = sess.features_untracked().unordered_const_ty_params();
|
2021-06-09 21:09:12 +00:00
|
|
|
(ParamKindOrd::Const { unordered }, format!("const {}: {}", ident, ty))
|
2020-12-30 16:43:30 +00:00
|
|
|
}
|
|
|
|
};
|
2021-06-09 21:09:12 +00:00
|
|
|
param_idents.push((kind, ord_kind, bounds, idx, ident));
|
2019-02-05 15:52:17 +00:00
|
|
|
match max_param {
|
2021-06-09 21:09:12 +00:00
|
|
|
Some(max_param) if max_param > ord_kind => {
|
|
|
|
let entry = out_of_order.entry(ord_kind).or_insert((max_param, vec![]));
|
2019-02-05 15:52:17 +00:00
|
|
|
entry.1.push(span);
|
|
|
|
}
|
2021-06-09 21:09:12 +00:00
|
|
|
Some(_) | None => max_param = Some(ord_kind),
|
2019-02-05 15:52:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if !out_of_order.is_empty() {
|
2021-06-09 21:09:12 +00:00
|
|
|
let mut ordered_params = "<".to_string();
|
2020-12-30 16:43:30 +00:00
|
|
|
param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i));
|
2019-02-05 15:52:17 +00:00
|
|
|
let mut first = true;
|
2020-12-30 16:43:30 +00:00
|
|
|
for (kind, _, 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;
|
2021-06-09 21:09:12 +00:00
|
|
|
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
ordered_params += ": ";
|
|
|
|
ordered_params += &pprust::bounds_to_string(&bounds);
|
2019-03-30 19:30:36 +00:00
|
|
|
}
|
2021-06-09 21:09:12 +00:00
|
|
|
|
2020-12-30 16:43:30 +00:00
|
|
|
match kind {
|
|
|
|
GenericParamKind::Type { default: Some(default) } => {
|
|
|
|
ordered_params += " = ";
|
|
|
|
ordered_params += &pprust::ty_to_string(default);
|
|
|
|
}
|
|
|
|
GenericParamKind::Type { default: None } => (),
|
|
|
|
GenericParamKind::Lifetime => (),
|
2021-05-29 02:54:32 +00:00
|
|
|
GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => {
|
|
|
|
ordered_params += " = ";
|
|
|
|
ordered_params += &pprust::expr_to_string(&*default.value);
|
|
|
|
}
|
|
|
|
GenericParamKind::Const { ty: _, kw_span: _, default: None } => (),
|
2020-12-30 16:43:30 +00:00
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2021-06-09 21:09:12 +00:00
|
|
|
ordered_params += ">";
|
|
|
|
|
|
|
|
for (param_ord, (max_param, spans)) in &out_of_order {
|
|
|
|
let mut err = handler.struct_span_err(
|
2020-01-21 23:07:07 +00:00
|
|
|
spans.clone(),
|
2019-03-30 19:57:04 +00:00
|
|
|
&format!(
|
2020-01-21 23:07:07 +00:00
|
|
|
"{} parameters must be declared prior to {} parameters",
|
|
|
|
param_ord, max_param,
|
2019-03-30 19:57:04 +00:00
|
|
|
),
|
2019-02-05 15:52:17 +00:00
|
|
|
);
|
2021-06-09 21:09:12 +00:00
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"reorder the parameters: lifetimes, {}",
|
|
|
|
if sess.features_untracked().unordered_const_ty_params() {
|
|
|
|
"then consts and types"
|
|
|
|
} else {
|
|
|
|
"then types, then consts"
|
|
|
|
}
|
|
|
|
),
|
|
|
|
ordered_params.clone(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
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) {
|
2021-08-08 14:49:13 +00:00
|
|
|
self.with_let_allowed(false, |this, let_allowed| match &expr.kind {
|
|
|
|
ExprKind::If(cond, then, opt_else) => {
|
|
|
|
this.visit_block(then);
|
|
|
|
walk_list!(this, visit_expr, opt_else);
|
|
|
|
this.with_let_allowed(true, |this, _| this.visit_expr(cond));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ExprKind::Let(..) if !let_allowed => this.ban_let_expr(expr),
|
|
|
|
ExprKind::LlvmInlineAsm(..) if !this.session.target.allow_asm => {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
2021-08-08 14:49:13 +00:00
|
|
|
this.session,
|
2019-12-31 20:25:16 +00:00
|
|
|
expr.span,
|
|
|
|
E0472,
|
2020-01-14 13:40:42 +00:00
|
|
|
"llvm_asm! is unsupported on this target"
|
2019-12-31 20:25:16 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2019-06-17 05:36:45 +00:00
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
ExprKind::Match(expr, arms) => {
|
|
|
|
this.visit_expr(expr);
|
|
|
|
for arm in arms {
|
|
|
|
this.visit_expr(&arm.body);
|
|
|
|
this.visit_pat(&arm.pat);
|
|
|
|
walk_list!(this, visit_attribute, &arm.attrs);
|
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
if let ExprKind::Let(_, ref expr, _) = guard.kind {
|
|
|
|
this.with_let_allowed(true, |this, _| this.visit_expr(expr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExprKind::Paren(_) | ExprKind::Binary(Spanned { node: BinOpKind::And, .. }, ..) => {
|
|
|
|
this.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ExprKind::While(cond, then, opt_label) => {
|
|
|
|
walk_list!(this, visit_label, opt_label);
|
|
|
|
this.visit_block(then);
|
|
|
|
this.with_let_allowed(true, |this, _| this.visit_expr(cond));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => visit::walk_expr(this, 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) {
|
2021-05-16 14:51:00 +00:00
|
|
|
self.visit_ty_common(ty);
|
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);
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:51:00 +00:00
|
|
|
fn visit_field_def(&mut self, s: &'a FieldDef) {
|
|
|
|
visit::walk_field_def(self, s)
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_item(&mut self, item: &'a Item) {
|
2020-07-30 01:27:50 +00:00
|
|
|
if item.attrs.iter().any(|attr| self.session.is_proc_macro_attr(attr)) {
|
2019-01-17 06:28:39 +00:00
|
|
|
self.has_proc_macro_decls = true;
|
|
|
|
}
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
if self.session.contains_name(&item.attrs, sym::no_mangle) {
|
2020-05-16 06:09:01 +00:00
|
|
|
self.check_nomangle_item_asciionly(item.ident, item.span);
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2021-01-29 07:31:08 +00:00
|
|
|
ItemKind::Impl(box ImplKind {
|
2020-01-14 04:30:20 +00:00
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness: _,
|
2021-08-25 11:53:16 +00:00
|
|
|
constness,
|
|
|
|
ref generics,
|
2020-03-05 00:15:23 +00:00
|
|
|
of_trait: Some(ref t),
|
2020-01-14 04:30:20 +00:00
|
|
|
ref self_ty,
|
2021-08-25 11:53:16 +00:00
|
|
|
ref items,
|
2021-01-29 07:31:08 +00:00
|
|
|
}) => {
|
2021-08-25 11:53:16 +00:00
|
|
|
self.with_in_trait_impl(true, Some(constness), |this| {
|
2020-02-05 11:27:45 +00:00
|
|
|
this.invalid_visibility(&item.vis, None);
|
|
|
|
if let TyKind::Err = self_ty.kind {
|
|
|
|
this.err_handler()
|
|
|
|
.struct_span_err(
|
|
|
|
item.span,
|
|
|
|
"`impl Trait for .. {}` is an obsolete syntax",
|
|
|
|
)
|
|
|
|
.help("use `auto trait Trait {}` instead")
|
|
|
|
.emit();
|
|
|
|
}
|
2020-03-05 00:15:23 +00:00
|
|
|
if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) {
|
2020-02-05 11:27:45 +00:00
|
|
|
struct_span_err!(
|
|
|
|
this.session,
|
2020-03-05 00:15:23 +00:00
|
|
|
sp.to(t.path.span),
|
2020-02-05 11:27:45 +00:00
|
|
|
E0198,
|
|
|
|
"negative impls cannot be unsafe"
|
|
|
|
)
|
2020-03-05 23:39:35 +00:00
|
|
|
.span_label(sp, "negative because of this")
|
2020-01-30 01:42:33 +00:00
|
|
|
.span_label(span, "unsafe because of this")
|
2019-12-24 22:38:22 +00:00
|
|
|
.emit();
|
2020-02-05 11:27:45 +00:00
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
this.visit_vis(&item.vis);
|
|
|
|
this.visit_ident(item.ident);
|
|
|
|
if let Const::Yes(_) = constness {
|
|
|
|
this.with_tilde_const_allowed(|this| this.visit_generics(generics));
|
|
|
|
} else {
|
|
|
|
this.visit_generics(generics);
|
|
|
|
}
|
|
|
|
this.visit_trait_ref(t);
|
|
|
|
this.visit_ty(self_ty);
|
|
|
|
|
|
|
|
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl);
|
2020-02-05 11:27:45 +00:00
|
|
|
});
|
2020-01-29 23:18:54 +00:00
|
|
|
return; // Avoid visiting again.
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
ItemKind::Impl(box ImplKind {
|
2020-01-14 04:30:20 +00:00
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
2020-01-14 04:30:25 +00:00
|
|
|
constness,
|
2020-01-14 04:30:20 +00:00
|
|
|
generics: _,
|
|
|
|
of_trait: None,
|
2020-03-05 00:15:23 +00:00
|
|
|
ref self_ty,
|
2020-01-14 04:30:20 +00:00
|
|
|
items: _,
|
2021-01-29 07:31:08 +00:00
|
|
|
}) => {
|
2020-03-06 19:58:52 +00:00
|
|
|
let error = |annotation_span, annotation| {
|
2020-03-05 23:39:35 +00:00
|
|
|
let mut err = self.err_handler().struct_span_err(
|
|
|
|
self_ty.span,
|
|
|
|
&format!("inherent impls cannot be {}", annotation),
|
|
|
|
);
|
|
|
|
err.span_label(annotation_span, &format!("{} because of this", annotation));
|
|
|
|
err.span_label(self_ty.span, "inherent impl for this type");
|
2020-03-06 19:58:52 +00:00
|
|
|
err
|
2020-03-05 23:39:35 +00:00
|
|
|
};
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
self.invalid_visibility(
|
|
|
|
&item.vis,
|
|
|
|
Some("place qualifiers on individual impl items instead"),
|
|
|
|
);
|
2020-01-30 01:42:33 +00:00
|
|
|
if let Unsafe::Yes(span) = unsafety {
|
2020-03-06 19:58:52 +00:00
|
|
|
error(span, "unsafe").code(error_code!(E0197)).emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
2020-03-05 00:15:23 +00:00
|
|
|
if let ImplPolarity::Negative(span) = polarity {
|
2020-03-06 19:58:52 +00:00
|
|
|
error(span, "negative").emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
2020-02-22 01:01:53 +00:00
|
|
|
if let Defaultness::Default(def_span) = defaultness {
|
2020-03-06 19:58:52 +00:00
|
|
|
error(def_span, "`default`")
|
2020-02-22 01:01:53 +00:00
|
|
|
.note("only trait implementations may be annotated with `default`")
|
2019-12-24 22:38:22 +00:00
|
|
|
.emit();
|
2017-12-02 19:15:03 +00:00
|
|
|
}
|
2020-01-30 01:42:33 +00:00
|
|
|
if let Const::Yes(span) = constness {
|
2020-03-06 19:58:52 +00:00
|
|
|
error(span, "`const`")
|
2020-01-14 04:30:25 +00:00
|
|
|
.note("only trait implementations may be annotated with `const`")
|
|
|
|
.emit();
|
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
2021-08-25 11:53:16 +00:00
|
|
|
ItemKind::Fn(box FnKind(def, ref sig, ref generics, ref body)) => {
|
2020-02-23 09:24:30 +00:00
|
|
|
self.check_defaultness(item.span, def);
|
2020-01-29 23:18:54 +00:00
|
|
|
|
|
|
|
if body.is_none() {
|
|
|
|
let msg = "free function without a body";
|
|
|
|
self.error_item_without_body(item.span, "function", msg, " { <body> }");
|
2019-12-02 01:38:33 +00:00
|
|
|
}
|
2021-08-25 11:53:16 +00:00
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
|
|
|
if let Const::Yes(_) = sig.header.constness {
|
|
|
|
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
|
|
|
|
} else {
|
|
|
|
self.visit_generics(generics);
|
|
|
|
}
|
|
|
|
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
|
|
|
|
self.visit_fn(kind, item.span, item.id);
|
|
|
|
walk_list!(self, visit_attribute, &item.attrs);
|
2021-08-25 13:38:14 +00:00
|
|
|
return; // Avoid visiting again.
|
2019-02-05 15:52:17 +00:00
|
|
|
}
|
2021-07-08 19:58:05 +00:00
|
|
|
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
|
2020-01-29 23:18:54 +00:00
|
|
|
let old_item = mem::replace(&mut self.extern_mod, Some(item));
|
2018-01-29 05:12:09 +00:00
|
|
|
self.invalid_visibility(
|
|
|
|
&item.vis,
|
|
|
|
Some("place qualifiers on individual foreign items instead"),
|
|
|
|
);
|
2020-08-23 10:42:19 +00:00
|
|
|
if let Unsafe::Yes(span) = unsafety {
|
|
|
|
self.err_handler().span_err(span, "extern block cannot be declared unsafe");
|
|
|
|
}
|
2021-07-08 19:58:05 +00:00
|
|
|
if abi.is_none() {
|
|
|
|
self.maybe_lint_missing_abi(item.span, item.id);
|
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
visit::walk_item(self, item);
|
|
|
|
self.extern_mod = old_item;
|
|
|
|
return; // Avoid visiting again.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
ItemKind::Trait(box TraitKind(
|
|
|
|
is_auto,
|
|
|
|
_,
|
|
|
|
ref generics,
|
|
|
|
ref bounds,
|
|
|
|
ref trait_items,
|
|
|
|
)) => {
|
2017-10-15 18:03:03 +00:00
|
|
|
if is_auto == IsAuto::Yes {
|
|
|
|
// Auto traits cannot have generics, super traits nor contain items.
|
2020-03-11 16:17:55 +00:00
|
|
|
self.deny_generic_params(generics, item.ident.span);
|
|
|
|
self.deny_super_traits(bounds, item.ident.span);
|
2021-10-03 15:58:10 +00:00
|
|
|
self.deny_where_clause(&generics.where_clause, item.ident.span);
|
2020-03-11 16:17:55 +00:00
|
|
|
self.deny_items(trait_items, item.ident.span);
|
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.
|
2020-01-05 02:34:10 +00:00
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
2020-01-05 00:29:45 +00:00
|
|
|
self.visit_generics(generics);
|
2021-08-25 11:53:16 +00:00
|
|
|
self.with_banned_tilde_const(|this| walk_list!(this, visit_param_bound, bounds));
|
2020-01-29 23:18:54 +00:00
|
|
|
walk_list!(self, visit_assoc_item, trait_items, AssocCtxt::Trait);
|
2020-01-05 02:34:10 +00:00
|
|
|
walk_list!(self, visit_attribute, &item.attrs);
|
2020-01-05 00:29:45 +00:00
|
|
|
return;
|
2016-08-07 21:33:35 +00:00
|
|
|
}
|
2021-02-16 21:56:07 +00:00
|
|
|
ItemKind::Mod(unsafety, ref mod_kind) => {
|
2020-08-23 10:42:19 +00:00
|
|
|
if let Unsafe::Yes(span) = unsafety {
|
|
|
|
self.err_handler().span_err(span, "module cannot be declared unsafe");
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
|
2021-02-16 21:56:07 +00:00
|
|
|
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _))
|
|
|
|
&& !self.session.contains_name(&item.attrs, sym::path)
|
|
|
|
{
|
2020-05-16 06:09:01 +00:00
|
|
|
self.check_mod_file_item_asciionly(item.ident);
|
|
|
|
}
|
2016-08-12 08:15:40 +00:00
|
|
|
}
|
2021-05-16 14:51:00 +00:00
|
|
|
ItemKind::Struct(ref vdata, ref generics) => match vdata {
|
|
|
|
// Duplicating the `Visitor` logic allows catching all cases
|
|
|
|
// of `Anonymous(Struct, Union)` outside of a field struct or union.
|
|
|
|
//
|
|
|
|
// Inside `visit_ty` the validator catches every `Anonymous(Struct, Union)` it
|
|
|
|
// encounters, and only on `ItemKind::Struct` and `ItemKind::Union`
|
|
|
|
// it uses `visit_ty_common`, which doesn't contain that specific check.
|
|
|
|
VariantData::Struct(ref fields, ..) => {
|
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
|
|
|
self.visit_generics(generics);
|
|
|
|
self.with_banned_assoc_ty_bound(|this| {
|
|
|
|
walk_list!(this, visit_struct_field_def, fields);
|
|
|
|
});
|
|
|
|
walk_list!(self, visit_attribute, &item.attrs);
|
|
|
|
return;
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2021-05-16 14:51:00 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
ItemKind::Union(ref vdata, ref generics) => {
|
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
|
|
|
}
|
2021-05-16 14:51:00 +00:00
|
|
|
match vdata {
|
|
|
|
VariantData::Struct(ref fields, ..) => {
|
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
|
|
|
self.visit_generics(generics);
|
|
|
|
self.with_banned_assoc_ty_bound(|this| {
|
|
|
|
walk_list!(this, visit_struct_field_def, fields);
|
|
|
|
});
|
|
|
|
walk_list!(self, visit_attribute, &item.attrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2020-02-23 09:24:30 +00:00
|
|
|
ItemKind::Const(def, .., None) => {
|
|
|
|
self.check_defaultness(item.span, def);
|
2020-02-14 13:21:02 +00:00
|
|
|
let msg = "free constant item without body";
|
|
|
|
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
|
|
|
|
}
|
|
|
|
ItemKind::Static(.., None) => {
|
|
|
|
let msg = "free static item without body";
|
|
|
|
self.error_item_without_body(item.span, "static", msg, " = <expr>;");
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
ItemKind::TyAlias(box TyAliasKind(def, _, ref bounds, ref body)) => {
|
2020-02-23 09:24:30 +00:00
|
|
|
self.check_defaultness(item.span, def);
|
2020-02-21 22:00:27 +00:00
|
|
|
if body.is_none() {
|
|
|
|
let msg = "free type alias without body";
|
|
|
|
self.error_item_without_body(item.span, "type", msg, " = <type>;");
|
|
|
|
}
|
|
|
|
self.check_type_no_bounds(bounds, "this context");
|
|
|
|
}
|
2016-05-22 15:07:28 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
visit::walk_item(self, item);
|
2016-05-22 15:07:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
|
2020-01-29 23:18:54 +00:00
|
|
|
match &fi.kind {
|
2021-01-29 07:31:08 +00:00
|
|
|
ForeignItemKind::Fn(box FnKind(def, sig, _, body)) => {
|
2020-02-23 09:24:30 +00:00
|
|
|
self.check_defaultness(fi.span, *def);
|
2020-01-29 23:18:54 +00:00
|
|
|
self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
|
|
|
|
self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
|
2021-04-06 15:49:59 +00:00
|
|
|
self.check_foreign_item_ascii_only(fi.ident);
|
2016-07-16 21:15:15 +00:00
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => {
|
2020-02-23 09:24:30 +00:00
|
|
|
self.check_defaultness(fi.span, *def);
|
2020-02-14 14:56:05 +00:00
|
|
|
self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span));
|
2020-02-14 11:55:42 +00:00
|
|
|
self.check_type_no_bounds(bounds, "`extern` blocks");
|
|
|
|
self.check_foreign_ty_genericless(generics);
|
2021-04-06 15:49:59 +00:00
|
|
|
self.check_foreign_item_ascii_only(fi.ident);
|
2020-02-14 11:55:42 +00:00
|
|
|
}
|
2020-02-14 14:56:05 +00:00
|
|
|
ForeignItemKind::Static(_, _, body) => {
|
|
|
|
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
|
2021-04-06 15:49:59 +00:00
|
|
|
self.check_foreign_item_ascii_only(fi.ident);
|
2020-02-14 14:56:05 +00:00
|
|
|
}
|
2020-02-29 16:32:20 +00:00
|
|
|
ForeignItemKind::MacCall(..) => {}
|
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) => {
|
2020-03-22 03:40:05 +00:00
|
|
|
self.check_generic_args_before_constraints(data);
|
|
|
|
|
|
|
|
for arg in &data.args {
|
|
|
|
match arg {
|
|
|
|
AngleBracketedArg::Arg(arg) => self.visit_generic_arg(arg),
|
|
|
|
// Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
|
|
|
|
// are allowed to contain nested `impl Trait`.
|
|
|
|
AngleBracketedArg::Constraint(constraint) => {
|
|
|
|
self.with_impl_trait(None, |this| {
|
|
|
|
this.visit_assoc_ty_constraint_from_generic_args(constraint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 11:35:14 +00:00
|
|
|
}
|
|
|
|
GenericArgs::Parenthesized(ref data) => {
|
|
|
|
walk_list!(self, visit_ty, &data.inputs);
|
2020-02-15 03:10:59 +00:00
|
|
|
if let FnRetTy::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) {
|
2021-08-26 19:12:02 +00:00
|
|
|
let cg_defaults = self.session.features_untracked().unordered_const_ty_params();
|
2021-03-01 11:50:09 +00:00
|
|
|
|
|
|
|
let mut prev_param_default = None;
|
2018-05-27 15:56:01 +00:00
|
|
|
for param in &generics.params {
|
2020-07-22 20:58:54 +00:00
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Lifetime => (),
|
2021-03-01 11:50:09 +00:00
|
|
|
GenericParamKind::Type { default: Some(_), .. }
|
|
|
|
| GenericParamKind::Const { default: Some(_), .. } => {
|
|
|
|
prev_param_default = Some(param.ident.span);
|
2020-07-22 20:58:54 +00:00
|
|
|
}
|
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
2021-03-01 11:50:09 +00:00
|
|
|
if let Some(span) = prev_param_default {
|
2020-07-22 20:58:54 +00:00
|
|
|
let mut err = self.err_handler().struct_span_err(
|
|
|
|
span,
|
2021-03-01 11:50:09 +00:00
|
|
|
"generic parameters with a default must be trailing",
|
2020-07-22 20:58:54 +00:00
|
|
|
);
|
2021-03-01 11:50:09 +00:00
|
|
|
if matches!(param.kind, GenericParamKind::Const { .. }) && !cg_defaults {
|
2020-07-22 20:58:54 +00:00
|
|
|
err.note(
|
|
|
|
"using type defaults and const parameters \
|
2020-07-23 06:06:42 +00:00
|
|
|
in the same parameter list is currently not permitted",
|
2020-07-22 20:58:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
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
|
|
|
|
2020-01-21 23:07:07 +00:00
|
|
|
validate_generic_param_order(
|
2019-03-30 19:57:04 +00:00
|
|
|
self.session,
|
|
|
|
self.err_handler(),
|
2020-12-30 16:43:30 +00:00
|
|
|
&generics.params,
|
2019-03-30 19:57:04 +00:00
|
|
|
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 {
|
2020-04-08 18:10:16 +00:00
|
|
|
deny_equality_constraints(self, predicate, generics);
|
2017-01-17 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 07:12:04 +00:00
|
|
|
walk_list!(self, visit_generic_param, &generics.params);
|
|
|
|
for predicate in &generics.where_clause.predicates {
|
|
|
|
match predicate {
|
|
|
|
WherePredicate::BoundPredicate(bound_pred) => {
|
|
|
|
// A type binding, eg `for<'c> Foo: Send+Clone+'c`
|
|
|
|
self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params);
|
2019-02-05 15:52:17 +00:00
|
|
|
|
2021-04-21 15:49:59 +00:00
|
|
|
// This is slightly complicated. Our representation for poly-trait-refs contains a single
|
|
|
|
// binder and thus we only allow a single level of quantification. However,
|
|
|
|
// the syntax of Rust permits quantification in two places in where clauses,
|
|
|
|
// e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
|
|
|
|
// defined, then error.
|
|
|
|
if !bound_pred.bound_generic_params.is_empty() {
|
|
|
|
for bound in &bound_pred.bounds {
|
|
|
|
match bound {
|
|
|
|
GenericBound::Trait(t, _) => {
|
|
|
|
if !t.bound_generic_params.is_empty() {
|
|
|
|
struct_span_err!(
|
|
|
|
self.err_handler(),
|
|
|
|
t.span,
|
|
|
|
E0316,
|
|
|
|
"nested quantification of lifetimes"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GenericBound::Outlives(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 07:12:04 +00:00
|
|
|
}
|
2021-04-21 15:49:59 +00:00
|
|
|
_ => {}
|
2021-04-21 07:12:04 +00:00
|
|
|
}
|
2021-04-21 15:49:59 +00:00
|
|
|
self.visit_where_predicate(predicate);
|
2021-04-21 07:12:04 +00:00
|
|
|
}
|
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) {
|
2020-01-20 09:20:45 +00:00
|
|
|
match bound {
|
|
|
|
GenericBound::Trait(_, TraitBoundModifier::MaybeConst) => {
|
2021-08-25 11:53:16 +00:00
|
|
|
if !self.is_tilde_const_allowed {
|
2021-08-25 13:38:14 +00:00
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(bound.span(), "`~const` is not allowed here")
|
2021-08-28 07:37:30 +00:00
|
|
|
.note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions")
|
2021-08-25 13:38:14 +00:00
|
|
|
.emit();
|
2020-01-05 00:29:45 +00:00
|
|
|
}
|
2020-01-20 09:20:45 +00:00
|
|
|
}
|
2020-01-05 00:29:45 +00:00
|
|
|
|
2020-01-20 09:20:45 +00:00
|
|
|
GenericBound::Trait(_, TraitBoundModifier::MaybeConstMaybe) => {
|
|
|
|
self.err_handler()
|
2021-08-25 11:53:16 +00:00
|
|
|
.span_err(bound.span(), "`~const` and `?` are mutually exclusive");
|
2020-01-05 00:29:45 +00:00
|
|
|
}
|
2020-01-20 09:20:45 +00:00
|
|
|
|
|
|
|
_ => {}
|
2020-01-05 00:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_param_bound(self, bound)
|
|
|
|
}
|
|
|
|
|
2017-06-24 18:26:04 +00:00
|
|
|
fn visit_pat(&mut self, pat: &'a Pat) {
|
2021-01-02 19:09:17 +00:00
|
|
|
match &pat.kind {
|
|
|
|
PatKind::Lit(expr) => {
|
2017-08-13 13:59:54 +00:00
|
|
|
self.check_expr_within_pat(expr, false);
|
2017-06-24 18:26:04 +00:00
|
|
|
}
|
2021-01-02 19:09:17 +00:00
|
|
|
PatKind::Range(start, end, _) => {
|
2019-12-11 09:04:34 +00:00
|
|
|
if let Some(expr) = start {
|
|
|
|
self.check_expr_within_pat(expr, true);
|
|
|
|
}
|
|
|
|
if let Some(expr) = end {
|
|
|
|
self.check_expr_within_pat(expr, true);
|
|
|
|
}
|
2017-06-24 18:26:04 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_pat(self, pat)
|
|
|
|
}
|
2018-03-06 10:22:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
|
|
|
|
// Only associated `fn`s can have `self` parameters.
|
|
|
|
let self_semantic = match fk.ctxt() {
|
|
|
|
Some(FnCtxt::Assoc(_)) => SelfSemantic::Yes,
|
|
|
|
_ => SelfSemantic::No,
|
|
|
|
};
|
|
|
|
self.check_fn_decl(fk.decl(), self_semantic);
|
|
|
|
|
|
|
|
self.check_c_varadic_type(fk);
|
|
|
|
|
2020-01-30 12:02:06 +00:00
|
|
|
// Functions cannot both be `const async`
|
|
|
|
if let Some(FnHeader {
|
|
|
|
constness: Const::Yes(cspan),
|
|
|
|
asyncness: Async::Yes { span: aspan, .. },
|
|
|
|
..
|
|
|
|
}) = fk.header()
|
|
|
|
{
|
|
|
|
self.err_handler()
|
2020-03-06 18:55:21 +00:00
|
|
|
.struct_span_err(
|
|
|
|
vec![*cspan, *aspan],
|
|
|
|
"functions cannot be both `const` and `async`",
|
|
|
|
)
|
2020-01-30 12:02:06 +00:00
|
|
|
.span_label(*cspan, "`const` because of this")
|
|
|
|
.span_label(*aspan, "`async` because of this")
|
2020-03-06 18:55:21 +00:00
|
|
|
.span_label(span, "") // Point at the fn header.
|
2020-01-30 12:02:06 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:58:05 +00:00
|
|
|
if let FnKind::Fn(
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit, .. }, .. },
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
) = fk
|
|
|
|
{
|
|
|
|
self.maybe_lint_missing_abi(*sig_span, id);
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
// Functions without bodies cannot have patterns.
|
|
|
|
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
|
2020-11-30 14:24:08 +00:00
|
|
|
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
|
2020-01-29 23:18:54 +00:00
|
|
|
let (code, msg, label) = match ctxt {
|
|
|
|
FnCtxt::Foreign => (
|
|
|
|
error_code!(E0130),
|
|
|
|
"patterns aren't allowed in foreign function declarations",
|
|
|
|
"pattern not allowed in foreign function",
|
|
|
|
),
|
|
|
|
_ => (
|
|
|
|
error_code!(E0642),
|
|
|
|
"patterns aren't allowed in functions without bodies",
|
|
|
|
"pattern not allowed in function without body",
|
|
|
|
),
|
|
|
|
};
|
|
|
|
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
|
2020-11-30 14:24:08 +00:00
|
|
|
if let Some(ident) = ident {
|
|
|
|
let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident);
|
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
|
|
|
PATTERNS_IN_FNS_WITHOUT_BODY,
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
msg,
|
|
|
|
diag,
|
|
|
|
)
|
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
} else {
|
|
|
|
self.err_handler()
|
|
|
|
.struct_span_err(span, msg)
|
|
|
|
.span_label(span, label)
|
|
|
|
.code(code)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
});
|
2019-06-09 10:58:40 +00:00
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
|
|
|
|
visit::walk_fn(self, fk, span);
|
2019-06-09 10:58:40 +00:00
|
|
|
}
|
2019-11-07 10:26:36 +00:00
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
|
2021-08-07 18:10:57 +00:00
|
|
|
if self.session.contains_name(&item.attrs, sym::no_mangle) {
|
|
|
|
self.check_nomangle_item_asciionly(item.ident, item.span);
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:24:30 +00:00
|
|
|
if ctxt == AssocCtxt::Trait || !self.in_trait_impl {
|
|
|
|
self.check_defaultness(item.span, item.kind.defaultness());
|
2019-12-02 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
if ctxt == AssocCtxt::Impl {
|
|
|
|
match &item.kind {
|
2020-02-23 09:24:30 +00:00
|
|
|
AssocItemKind::Const(_, _, body) => {
|
2020-01-29 23:18:54 +00:00
|
|
|
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
AssocItemKind::Fn(box FnKind(_, _, _, body)) => {
|
2020-01-29 23:18:54 +00:00
|
|
|
self.check_impl_item_provided(item.span, body, "function", " { <body> }");
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
AssocItemKind::TyAlias(box TyAliasKind(_, _, bounds, body)) => {
|
2020-01-29 23:18:54 +00:00
|
|
|
self.check_impl_item_provided(item.span, body, "type", " = <type>;");
|
2020-02-14 11:55:42 +00:00
|
|
|
self.check_type_no_bounds(bounds, "`impl`s");
|
2020-01-29 23:18:54 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
if ctxt == AssocCtxt::Trait || self.in_trait_impl {
|
|
|
|
self.invalid_visibility(&item.vis, None);
|
2021-01-29 07:31:08 +00:00
|
|
|
if let AssocItemKind::Fn(box FnKind(_, sig, _, _)) = &item.kind {
|
2020-01-29 23:18:54 +00:00
|
|
|
self.check_trait_fn_not_const(sig.header.constness);
|
2020-01-30 04:31:04 +00:00
|
|
|
self.check_trait_fn_not_async(item.span, sig.header.asyncness);
|
2020-01-29 23:18:54 +00:00
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
|
2020-02-15 21:21:00 +00:00
|
|
|
if let AssocItemKind::Const(..) = item.kind {
|
|
|
|
self.check_item_named(item.ident, "const");
|
2020-02-14 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
match item.kind {
|
|
|
|
AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty))
|
|
|
|
if ctxt == AssocCtxt::Trait =>
|
|
|
|
{
|
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
|
|
|
walk_list!(self, visit_attribute, &item.attrs);
|
|
|
|
self.with_tilde_const_allowed(|this| {
|
|
|
|
this.visit_generics(generics);
|
|
|
|
walk_list!(this, visit_param_bound, bounds);
|
|
|
|
});
|
|
|
|
walk_list!(self, visit_ty, ty);
|
|
|
|
}
|
|
|
|
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body))
|
2021-09-03 10:37:57 +00:00
|
|
|
if self.in_const_trait_impl
|
|
|
|
|| ctxt == AssocCtxt::Trait
|
|
|
|
|| matches!(sig.header.constness, Const::Yes(_)) =>
|
2021-08-25 11:53:16 +00:00
|
|
|
{
|
|
|
|
self.visit_vis(&item.vis);
|
|
|
|
self.visit_ident(item.ident);
|
|
|
|
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
|
|
|
|
let kind =
|
|
|
|
FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
|
|
|
|
self.visit_fn(kind, item.span, item.id);
|
|
|
|
}
|
|
|
|
_ => self
|
|
|
|
.with_in_trait_impl(false, None, |this| visit::walk_assoc_item(this, item, ctxt)),
|
|
|
|
}
|
2019-12-02 01:38:33 +00:00
|
|
|
}
|
2016-05-22 14:51:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 18:09:10 +00:00
|
|
|
/// When encountering an equality constraint in a `where` clause, emit an error. If the code seems
|
|
|
|
/// like it's setting an associated type, provide an appropriate suggestion.
|
2020-04-08 18:10:16 +00:00
|
|
|
fn deny_equality_constraints(
|
|
|
|
this: &mut AstValidator<'_>,
|
|
|
|
predicate: &WhereEqPredicate,
|
|
|
|
generics: &Generics,
|
|
|
|
) {
|
|
|
|
let mut err = this.err_handler().struct_span_err(
|
|
|
|
predicate.span,
|
|
|
|
"equality constraints are not yet supported in `where` clauses",
|
|
|
|
);
|
|
|
|
err.span_label(predicate.span, "not supported");
|
|
|
|
|
|
|
|
// Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
|
|
|
|
if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind {
|
|
|
|
if let TyKind::Path(None, path) = &qself.ty.kind {
|
|
|
|
match &path.segments[..] {
|
|
|
|
[PathSegment { ident, args: None, .. }] => {
|
|
|
|
for param in &generics.params {
|
|
|
|
if param.ident == *ident {
|
|
|
|
let param = ident;
|
|
|
|
match &full_path.segments[qself.position..] {
|
2020-11-19 17:28:38 +00:00
|
|
|
[PathSegment { ident, args, .. }] => {
|
2020-04-08 18:10:16 +00:00
|
|
|
// Make a new `Path` from `foo::Bar` to `Foo<Bar = RhsTy>`.
|
|
|
|
let mut assoc_path = full_path.clone();
|
|
|
|
// Remove `Bar` from `Foo::Bar`.
|
|
|
|
assoc_path.segments.pop();
|
|
|
|
let len = assoc_path.segments.len() - 1;
|
2020-11-19 17:28:38 +00:00
|
|
|
let gen_args = args.as_ref().map(|p| (**p).clone());
|
2020-04-08 18:10:16 +00:00
|
|
|
// Build `<Bar = RhsTy>`.
|
|
|
|
let arg = AngleBracketedArg::Constraint(AssocTyConstraint {
|
|
|
|
id: rustc_ast::node_id::DUMMY_NODE_ID,
|
|
|
|
ident: *ident,
|
2020-11-19 17:28:38 +00:00
|
|
|
gen_args,
|
2020-04-08 18:10:16 +00:00
|
|
|
kind: AssocTyConstraintKind::Equality {
|
|
|
|
ty: predicate.rhs_ty.clone(),
|
|
|
|
},
|
|
|
|
span: ident.span,
|
|
|
|
});
|
|
|
|
// Add `<Bar = RhsTy>` to `Foo`.
|
|
|
|
match &mut assoc_path.segments[len].args {
|
|
|
|
Some(args) => match args.deref_mut() {
|
|
|
|
GenericArgs::Parenthesized(_) => continue,
|
|
|
|
GenericArgs::AngleBracketed(args) => {
|
|
|
|
args.args.push(arg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
empty_args => {
|
|
|
|
*empty_args = AngleBracketedArgs {
|
|
|
|
span: ident.span,
|
|
|
|
args: vec![arg],
|
|
|
|
}
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
predicate.span,
|
|
|
|
&format!(
|
|
|
|
"if `{}` is an associated type you're trying to set, \
|
|
|
|
use the associated type binding syntax",
|
|
|
|
ident
|
|
|
|
),
|
|
|
|
format!(
|
|
|
|
"{}: {}",
|
|
|
|
param,
|
|
|
|
pprust::path_to_string(&assoc_path)
|
|
|
|
),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.note(
|
|
|
|
"see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information",
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
|
2019-01-17 06:28:39 +00:00
|
|
|
let mut validator = AstValidator {
|
2019-01-18 11:35:14 +00:00
|
|
|
session,
|
2020-01-29 23:18:54 +00:00
|
|
|
extern_mod: None,
|
|
|
|
in_trait_impl: false,
|
2021-08-25 11:53:16 +00:00
|
|
|
in_const_trait_impl: false,
|
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,
|
2021-08-25 11:53:16 +00:00
|
|
|
is_tilde_const_allowed: false,
|
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,
|
2021-08-08 14:49:13 +00:00
|
|
|
is_let_allowed: 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
|
|
|
}
|