summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_ast_passes/src/ast_validation.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /compiler/rustc_ast_passes/src/ast_validation.rs
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast_passes/src/ast_validation.rs')
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs129
1 files changed, 73 insertions, 56 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index 096cea945..bd3e676da 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -13,6 +13,7 @@ use rustc_ast::*;
use rustc_ast::{walk_list, StaticItem};
use rustc_ast_pretty::pprust::{self, State};
use rustc_data_structures::fx::FxIndexMap;
+use rustc_feature::Features;
use rustc_macros::Subdiagnostic;
use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{
@@ -45,6 +46,7 @@ enum DisallowTildeConstContext<'a> {
struct AstValidator<'a> {
session: &'a Session,
+ features: &'a Features,
/// The span of the `extern` in an `extern { ... }` block, if any.
extern_mod: Option<&'a Item>,
@@ -136,40 +138,42 @@ impl<'a> AstValidator<'a> {
}
}
- fn check_gat_where(
+ fn check_type_alias_where_clause_location(
&mut self,
- id: NodeId,
- before_predicates: &[WherePredicate],
- where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause),
- ) {
- if !before_predicates.is_empty() {
- let mut state = State::new();
- if !where_clauses.1.0 {
- state.space();
- state.word_space("where");
- } else {
+ ty_alias: &TyAlias,
+ ) -> Result<(), errors::WhereClauseBeforeTypeAlias> {
+ let before_predicates =
+ ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_predicates_split).0;
+
+ if ty_alias.ty.is_none() || before_predicates.is_empty() {
+ return Ok(());
+ }
+
+ let mut state = State::new();
+ if !ty_alias.where_clauses.1.0 {
+ state.space();
+ state.word_space("where");
+ } else {
+ state.word_space(",");
+ }
+ let mut first = true;
+ for p in before_predicates {
+ if !first {
state.word_space(",");
}
- let mut first = true;
- for p in before_predicates.iter() {
- if !first {
- state.word_space(",");
- }
- first = false;
- state.print_where_predicate(p);
- }
- let suggestion = state.s.eof();
- self.lint_buffer.buffer_lint_with_diagnostic(
- DEPRECATED_WHERE_CLAUSE_LOCATION,
- id,
- where_clauses.0.1,
- fluent::ast_passes_deprecated_where_clause_location,
- BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(
- where_clauses.1.1.shrink_to_hi(),
- suggestion,
- ),
- );
+ first = false;
+ state.print_where_predicate(p);
}
+
+ let span = ty_alias.where_clauses.0.1;
+ Err(errors::WhereClauseBeforeTypeAlias {
+ span,
+ sugg: errors::WhereClauseBeforeTypeAliasSugg {
+ left: span,
+ snippet: state.s.eof(),
+ right: ty_alias.where_clauses.1.1.shrink_to_hi(),
+ },
+ })
}
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
@@ -659,7 +663,7 @@ fn validate_generic_param_order(
GenericParamKind::Type { .. } => (ParamKindOrd::TypeOrConst, ident.to_string()),
GenericParamKind::Const { ty, .. } => {
let ty = pprust::ty_to_string(ty);
- (ParamKindOrd::TypeOrConst, format!("const {}: {}", ident, ty))
+ (ParamKindOrd::TypeOrConst, format!("const {ident}: {ty}"))
}
};
param_idents.push((kind, ord_kind, bounds, idx, ident));
@@ -1009,7 +1013,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
replace_span: self.ending_semi_or_hi(item.span),
});
}
- ItemKind::TyAlias(box TyAlias { defaultness, where_clauses, bounds, ty, .. }) => {
+ ItemKind::TyAlias(
+ ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
+ ) => {
self.check_defaultness(item.span, *defaultness);
if ty.is_none() {
self.session.emit_err(errors::TyAliasWithoutBody {
@@ -1018,9 +1024,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
});
}
self.check_type_no_bounds(bounds, "this context");
- if where_clauses.1.0 {
- self.err_handler()
- .emit_err(errors::WhereAfterTypeAlias { span: where_clauses.1.1 });
+
+ if self.features.lazy_type_alias {
+ if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
+ self.err_handler().emit_err(err);
+ }
+ } else if where_clauses.1.0 {
+ self.err_handler().emit_err(errors::WhereClauseAfterTypeAlias {
+ span: where_clauses.1.1,
+ help: self.session.is_nightly_build().then_some(()),
+ });
}
}
_ => {}
@@ -1300,14 +1313,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
});
}
}
- AssocItemKind::Type(box TyAlias {
- generics,
- where_clauses,
- where_predicates_split,
- bounds,
- ty,
- ..
- }) => {
+ AssocItemKind::Type(box TyAlias { bounds, ty, .. }) => {
if ty.is_none() {
self.session.emit_err(errors::AssocTypeWithoutBody {
span: item.span,
@@ -1315,18 +1321,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
});
}
self.check_type_no_bounds(bounds, "`impl`s");
- if ty.is_some() {
- self.check_gat_where(
- item.id,
- generics.where_clause.predicates.split_at(*where_predicates_split).0,
- *where_clauses,
- );
- }
}
_ => {}
}
}
+ if let AssocItemKind::Type(ty_alias) = &item.kind
+ && let Err(err) = self.check_type_alias_where_clause_location(ty_alias)
+ {
+ self.lint_buffer.buffer_lint_with_diagnostic(
+ DEPRECATED_WHERE_CLAUSE_LOCATION,
+ item.id,
+ err.span,
+ fluent::ast_passes_deprecated_where_clause_location,
+ BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(
+ err.sugg.right,
+ err.sugg.snippet,
+ ),
+ );
+ }
+
if ctxt == AssocCtxt::Trait || self.in_trait_impl {
self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
@@ -1462,15 +1476,12 @@ fn deny_equality_constraints(
let Some(arg) = args.args.last() else {
continue;
};
- (
- format!(", {} = {}", assoc, ty),
- arg.span().shrink_to_hi(),
- )
+ (format!(", {assoc} = {ty}"), arg.span().shrink_to_hi())
}
_ => continue,
},
None => (
- format!("<{} = {}>", assoc, ty),
+ format!("<{assoc} = {ty}>"),
trait_segment.span().shrink_to_hi(),
),
};
@@ -1491,9 +1502,15 @@ fn deny_equality_constraints(
this.err_handler().emit_err(err);
}
-pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
+pub fn check_crate(
+ session: &Session,
+ features: &Features,
+ krate: &Crate,
+ lints: &mut LintBuffer,
+) -> bool {
let mut validator = AstValidator {
session,
+ features,
extern_mod: None,
in_trait_impl: false,
in_const_trait_impl: false,