Skip to content

Commit d9e8e42

Browse files
committed
Migrate check_type_no_bounds
1 parent 95b61d1 commit d9e8e42

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_session::lint::builtin::{
2020
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
2121
};
2222
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
23-
use rustc_session::Session;
23+
use rustc_session::{Session, SessionDiagnostic};
2424
use rustc_span::source_map::Spanned;
2525
use rustc_span::symbol::{kw, sym, Ident};
2626
use rustc_span::Span;
@@ -404,15 +404,16 @@ impl<'a> AstValidator<'a> {
404404
}
405405
}
406406

407-
fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) {
407+
fn check_type_no_bounds<D>(&self, bounds: &[GenericBound], create_diag: impl FnOnce(Span) -> D)
408+
where
409+
D: SessionDiagnostic<'a>,
410+
{
408411
let span = match bounds {
409412
[] => return,
410413
[b0] => b0.span(),
411414
[b0, .., bl] => b0.span().to(bl.span()),
412415
};
413-
self.err_handler()
414-
.struct_span_err(span, &format!("bounds on `type`s in {} have no effect", ctx))
415-
.emit();
416+
self.session.emit_err(create_diag(span));
416417
}
417418

418419
fn check_foreign_ty_genericless(&self, generics: &Generics, where_span: Span) {
@@ -1205,7 +1206,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12051206
replace_span: self.ending_semi_or_hi(item.span),
12061207
});
12071208
}
1208-
self.check_type_no_bounds(bounds, "this context");
1209+
self.check_type_no_bounds(bounds, |span| TyAliasWithBound { span });
12091210
if where_clauses.1.0 {
12101211
let mut err = self.err_handler().struct_span_err(
12111212
where_clauses.1.1,
@@ -1241,7 +1242,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12411242
}) => {
12421243
self.check_defaultness(fi.span, *defaultness);
12431244
self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
1244-
self.check_type_no_bounds(bounds, "`extern` blocks");
1245+
self.check_type_no_bounds(bounds, |span| ForeignTypeWithBound { span });
12451246
self.check_foreign_ty_genericless(generics, where_clauses.0.1);
12461247
self.check_foreign_item_ascii_only(fi.ident);
12471248
}
@@ -1539,7 +1540,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15391540
replace_span: self.ending_semi_or_hi(item.span),
15401541
});
15411542
}
1542-
self.check_type_no_bounds(bounds, "`impl`s");
1543+
self.check_type_no_bounds(bounds, |span| ImplAssocTypeWithBound { span });
15431544
if ty.is_some() {
15441545
self.check_gat_where(
15451546
item.id,

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,24 @@ pub enum ExternBlockSuggestion {
224224
abi: Symbol,
225225
},
226226
}
227+
228+
#[derive(SessionDiagnostic)]
229+
#[diag(ast_passes::ty_alias_with_bound)]
230+
pub struct TyAliasWithBound {
231+
#[primary_span]
232+
pub span: Span,
233+
}
234+
235+
#[derive(SessionDiagnostic)]
236+
#[diag(ast_passes::foreign_type_with_bound)]
237+
pub struct ForeignTypeWithBound {
238+
#[primary_span]
239+
pub span: Span,
240+
}
241+
242+
#[derive(SessionDiagnostic)]
243+
#[diag(ast_passes::impl_assoc_type_with_bound)]
244+
pub struct ImplAssocTypeWithBound {
245+
#[primary_span]
246+
pub span: Span,
247+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ ast_passes_fn_without_body =
9090
.suggestion = provide a definition for the function
9191
9292
ast_passes_extern_block_suggestion = if you meant to declare an externally defined function, use an `extern` block
93+
94+
ast_passes_ty_alias_with_bound =
95+
bounds on `type`s in this context have no effect
96+
97+
ast_passes_foreign_type_with_bound =
98+
bounds on `type`s in `extern` blocks have no effect
99+
100+
ast_passes_impl_assoc_type_with_bound =
101+
bounds on `type`s in `impl`s have no effect

0 commit comments

Comments
 (0)