Skip to content

Commit e144a23

Browse files
committed
Migrate deprecated_where_clause_location, forbidden_assoc_constraint, keyword_lifetime, invalid_label, invalid_visibility
1 parent 80451de commit e144a23

File tree

4 files changed

+77
-26
lines changed

4 files changed

+77
-26
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast::walk_list;
1313
use rustc_ast::*;
1414
use rustc_ast_pretty::pprust::{self, State};
1515
use rustc_data_structures::fx::FxHashMap;
16-
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability, Diagnostic};
16+
use rustc_errors::{error_code, fluent, pluralize, struct_span_err, Applicability, Diagnostic};
1717
use rustc_parse::validate_attr;
1818
use rustc_session::lint::builtin::{
1919
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
@@ -27,7 +27,7 @@ use rustc_target::spec::abi;
2727
use std::mem;
2828
use std::ops::{Deref, DerefMut};
2929

30-
use crate::errors::ForbiddenLet;
30+
use crate::errors::*;
3131

3232
const MORE_EXTERN: &str =
3333
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
@@ -149,7 +149,7 @@ impl<'a> AstValidator<'a> {
149149
DEPRECATED_WHERE_CLAUSE_LOCATION,
150150
id,
151151
where_clauses.0.1,
152-
"where clause not allowed here",
152+
fluent::ast_passes::deprecated_where_clause_location,
153153
BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(
154154
where_clauses.1.1.shrink_to_hi(),
155155
suggestion,
@@ -179,10 +179,7 @@ impl<'a> AstValidator<'a> {
179179
AssocConstraintKind::Equality { .. } => {}
180180
AssocConstraintKind::Bound { .. } => {
181181
if self.is_assoc_ty_bound_banned {
182-
self.err_handler().span_err(
183-
constraint.span,
184-
"associated type bounds are not allowed within structs, enums, or unions",
185-
);
182+
self.session.emit_err(ForbiddenAssocConstraint { span: constraint.span });
186183
}
187184
}
188185
}
@@ -254,31 +251,26 @@ impl<'a> AstValidator<'a> {
254251
fn check_lifetime(&self, ident: Ident) {
255252
let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Empty];
256253
if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
257-
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
254+
self.session.emit_err(KeywordLifetime { span: ident.span });
258255
}
259256
}
260257

261258
fn check_label(&self, ident: Ident) {
262259
if ident.without_first_quote().is_reserved() {
263-
self.err_handler()
264-
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
260+
self.session.emit_err(InvalidLabel { span: ident.span, name: ident.name });
265261
}
266262
}
267263

268-
fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
264+
fn invalid_visibility(&self, vis: &Visibility, note: Option<InvalidVisibilityNote>) {
269265
if let VisibilityKind::Inherited = vis.kind {
270266
return;
271267
}
272268

273-
let mut err =
274-
struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier");
275-
if vis.kind.is_pub() {
276-
err.span_label(vis.span, "`pub` not permitted here because it's implied");
277-
}
278-
if let Some(note) = note {
279-
err.note(note);
280-
}
281-
err.emit();
269+
self.session.emit_err(InvalidVisibility {
270+
span: vis.span,
271+
implied: if vis.kind.is_pub() { Some(vis.span) } else { None },
272+
note,
273+
});
282274
}
283275

284276
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
@@ -1154,7 +1146,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11541146

11551147
self.invalid_visibility(
11561148
&item.vis,
1157-
Some("place qualifiers on individual impl items instead"),
1149+
Some(InvalidVisibilityNote::IndividualImplItems),
11581150
);
11591151
if let Unsafe::Yes(span) = unsafety {
11601152
error(span, "unsafe").code(error_code!(E0197)).emit();
@@ -1222,7 +1214,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12221214
let old_item = mem::replace(&mut self.extern_mod, Some(item));
12231215
self.invalid_visibility(
12241216
&item.vis,
1225-
Some("place qualifiers on individual foreign items instead"),
1217+
Some(InvalidVisibilityNote::IndividualForeignItems),
12261218
);
12271219
if let Unsafe::Yes(span) = unsafety {
12281220
self.err_handler().span_err(span, "extern block cannot be declared unsafe");

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use rustc_errors::fluent;
44
use rustc_errors::{AddSubdiagnostic, Diagnostic};
5-
use rustc_macros::SessionDiagnostic;
6-
use rustc_span::Span;
5+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
6+
use rustc_span::{Span, Symbol};
77

88
use crate::ast_validation::ForbiddenLetReason;
99

@@ -30,3 +30,44 @@ impl AddSubdiagnostic for ForbiddenLetReason {
3030
}
3131
}
3232
}
33+
34+
#[derive(SessionDiagnostic)]
35+
#[error(ast_passes::forbidden_assoc_constraint)]
36+
pub struct ForbiddenAssocConstraint {
37+
#[primary_span]
38+
pub span: Span,
39+
}
40+
41+
#[derive(SessionDiagnostic)]
42+
#[error(ast_passes::keyword_lifetime)]
43+
pub struct KeywordLifetime {
44+
#[primary_span]
45+
pub span: Span,
46+
}
47+
48+
#[derive(SessionDiagnostic)]
49+
#[error(ast_passes::invalid_label)]
50+
pub struct InvalidLabel {
51+
#[primary_span]
52+
pub span: Span,
53+
pub name: Symbol,
54+
}
55+
56+
#[derive(SessionDiagnostic)]
57+
#[error(ast_passes::invalid_visibility, code = "E0449")]
58+
pub struct InvalidVisibility {
59+
#[primary_span]
60+
pub span: Span,
61+
#[label(ast_passes::implied)]
62+
pub implied: Option<Span>,
63+
#[subdiagnostic]
64+
pub note: Option<InvalidVisibilityNote>,
65+
}
66+
67+
#[derive(SessionSubdiagnostic)]
68+
pub enum InvalidVisibilityNote {
69+
#[note(ast_passes::individual_impl_items)]
70+
IndividualImplItems,
71+
#[note(ast_passes::individual_foreign_items)]
72+
IndividualForeignItems,
73+
}

compiler/rustc_error_messages/locales/en-US/ast_passes.ftl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,21 @@ ast_passes_forbidden_let =
33
.note = only supported directly in conditions of `if` and `while` expressions
44
.not_supported_or = `||` operators are not supported in let chain expressions
55
.not_supported_parentheses = `let`s wrapped in parentheses are not supported in a context with let chains
6+
7+
ast_passes_deprecated_where_clause_location =
8+
where clause not allowed here
9+
10+
ast_passes_forbidden_assoc_constraint =
11+
associated type bounds are not allowed within structs, enums, or unions
12+
13+
ast_passes_keyword_lifetime =
14+
lifetimes cannot use keyword names
15+
16+
ast_passes_invalid_label =
17+
invalid label name `{$name}`
18+
19+
ast_passes_invalid_visibility =
20+
unnecessary visibility qualifier
21+
.implied = `pub` not permitted here because it's implied
22+
.individual_impl_items = place qualifiers on individual impl items instead
23+
.individual_foreign_items = place qualifiers on individual foreign items instead

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ impl LintBuffer {
537537
lint: &'static Lint,
538538
id: NodeId,
539539
sp: impl Into<MultiSpan>,
540-
msg: &str,
540+
msg: impl Into<DiagnosticMessage>,
541541
) {
542542
self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
543543
}
@@ -547,7 +547,7 @@ impl LintBuffer {
547547
lint: &'static Lint,
548548
id: NodeId,
549549
sp: impl Into<MultiSpan>,
550-
msg: &str,
550+
msg: impl Into<DiagnosticMessage>,
551551
diagnostic: BuiltinLintDiagnostics,
552552
) {
553553
self.add_lint(lint, id, sp.into(), msg, diagnostic)

0 commit comments

Comments
 (0)