Skip to content

Commit 647fdf7

Browse files
committed
Auto merge of rust-lang#127854 - fmease:glob-import-type_ir_inherent-lint, r=<try>
Add internal lint for detecting non-glob imports of `rustc_type_ir::inherent` rust-lang#127627 (comment) r? compiler-errors
2 parents e35364a + bd6a408 commit 647fdf7

File tree

9 files changed

+168
-2
lines changed

9 files changed

+168
-2
lines changed

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ lint_non_fmt_panic_unused =
556556
}
557557
.add_fmt_suggestion = or add a "{"{"}{"}"}" format string to use the message literally
558558
559+
lint_non_glob_import_type_ir_inherent = non-glob import of `rustc_type_ir::inherent`
560+
.suggestion = try using a glob import instead
561+
559562
lint_non_local_definitions_cargo_update = the {$macro_kind} `{$macro_name}` may come from an old version of the `{$crate_name}` crate, try updating your dependency with `cargo update -p {$crate_name}`
560563
561564
lint_non_local_definitions_deprecation = this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

compiler/rustc_lint/src/internal.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
44
use crate::lints::{
55
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
6-
QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
6+
NonGlobImportTypeIrInherent, QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag,
7+
TykindKind, UntranslatableDiag,
78
};
89
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
910
use rustc_ast as ast;
@@ -263,6 +264,49 @@ fn gen_args(segment: &PathSegment<'_>) -> String {
263264
String::new()
264265
}
265266

267+
declare_tool_lint! {
268+
/// The `non_glob_import_of_type_ir_inherent_item` lint detects
269+
/// non-glob imports of module `rustc_type_ir::inherent`.
270+
pub rustc::NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
271+
Allow,
272+
"non-glob import of `rustc_type_ir::inherent`",
273+
report_in_external_macro: true
274+
}
275+
276+
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT]);
277+
278+
impl<'tcx> LateLintPass<'tcx> for TypeIr {
279+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
280+
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
281+
282+
let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id);
283+
let (lo, hi, snippet) = match path.segments {
284+
[.., penultimate, segment]
285+
if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) =>
286+
{
287+
(segment.ident.span, item.ident.span, "*")
288+
}
289+
[.., segment]
290+
if path.res.iter().flat_map(Res::opt_def_id).any(is_mod_inherent)
291+
&& let rustc_hir::UseKind::Single = kind =>
292+
{
293+
let (lo, snippet) =
294+
match cx.tcx.sess.source_map().span_to_snippet(path.span).as_deref() {
295+
Ok("self") => (path.span, "*"),
296+
_ => (segment.ident.span.shrink_to_hi(), "::*"),
297+
};
298+
(lo, if segment.ident == item.ident { lo } else { item.ident.span }, snippet)
299+
}
300+
_ => return,
301+
};
302+
cx.emit_span_lint(
303+
NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
304+
path.span,
305+
NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet },
306+
);
307+
}
308+
}
309+
266310
declare_tool_lint! {
267311
/// The `lint_pass_impl_without_macro` detects manual implementations of a lint
268312
/// pass, without using [`declare_lint_pass`] or [`impl_lint_pass`].

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ fn register_internals(store: &mut LintStore) {
572572
store.register_late_mod_pass(|_| Box::new(ExistingDocKeyword));
573573
store.register_lints(&TyTyKind::get_lints());
574574
store.register_late_mod_pass(|_| Box::new(TyTyKind));
575+
store.register_lints(&TypeIr::get_lints());
576+
store.register_late_mod_pass(|_| Box::new(TypeIr));
575577
store.register_lints(&Diagnostics::get_lints());
576578
store.register_late_mod_pass(|_| Box::new(Diagnostics));
577579
store.register_lints(&BadOptAccess::get_lints());
@@ -595,6 +597,7 @@ fn register_internals(store: &mut LintStore) {
595597
LintId::of(PASS_BY_VALUE),
596598
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
597599
LintId::of(USAGE_OF_QUALIFIED_TY),
600+
LintId::of(NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT),
598601
LintId::of(EXISTING_DOC_KEYWORD),
599602
LintId::of(BAD_OPT_ACCESS),
600603
LintId::of(SPAN_USE_EQ_CTXT),

compiler/rustc_lint/src/lints.rs

+8
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@ pub struct TyQualified {
926926
pub suggestion: Span,
927927
}
928928

929+
#[derive(LintDiagnostic)]
930+
#[diag(lint_non_glob_import_type_ir_inherent)]
931+
pub struct NonGlobImportTypeIrInherent {
932+
#[suggestion(code = "{snippet}", applicability = "maybe-incorrect")]
933+
pub suggestion: Option<Span>,
934+
pub snippet: &'static str,
935+
}
936+
929937
#[derive(LintDiagnostic)]
930938
#[diag(lint_lintpass_by_hand)]
931939
#[help]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,7 @@ symbols! {
19261926
type_ascription,
19271927
type_changing_struct_update,
19281928
type_id,
1929+
type_ir_inherent,
19291930
type_length_limit,
19301931
type_macros,
19311932
type_name,

compiler/rustc_type_ir/src/effects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::inherent::{AdtDef, IntoKind, Ty};
1+
use crate::inherent::*;
22
use crate::lang_items::TraitSolverLangItem::{EffectsMaybe, EffectsNoRuntime, EffectsRuntime};
33
use crate::Interner;
44

compiler/rustc_type_ir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod elaborate;
2424
pub mod error;
2525
pub mod fast_reject;
2626
pub mod fold;
27+
#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_inherent")]
2728
pub mod inherent;
2829
pub mod ir_print;
2930
pub mod lang_items;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ compile-flags: -Z unstable-options
2+
//@ ignore-stage1 (can be removed after beta bump, cfg(not(bootstrap)))
3+
#![feature(rustc_private)]
4+
#![deny(rustc::non_glob_import_of_type_ir_inherent)]
5+
6+
extern crate rustc_type_ir;
7+
8+
mod ok {
9+
use rustc_type_ir::inherent::*; // OK
10+
use rustc_type_ir::inherent::{}; // OK
11+
use rustc_type_ir::inherent::{*}; // OK
12+
13+
fn usage<T: rustc_type_ir::inherent::SliceLike>() {} // OK
14+
}
15+
16+
mod direct {
17+
use rustc_type_ir::inherent::Predicate; //~ ERROR non-glob import of `rustc_type_ir::inherent`
18+
use rustc_type_ir::inherent::{AdtDef, Ty};
19+
//~^ ERROR non-glob import of `rustc_type_ir::inherent`
20+
//~| ERROR non-glob import of `rustc_type_ir::inherent`
21+
use rustc_type_ir::inherent::ParamEnv as _; //~ ERROR non-glob import of `rustc_type_ir::inherent`
22+
}
23+
24+
mod indirect0 {
25+
use rustc_type_ir::inherent; //~ ERROR non-glob import of `rustc_type_ir::inherent`
26+
use rustc_type_ir::inherent as inh; //~ ERROR non-glob import of `rustc_type_ir::inherent`
27+
use rustc_type_ir::{inherent as _}; //~ ERROR non-glob import of `rustc_type_ir::inherent`
28+
29+
fn usage0<T: inherent::SliceLike>() {}
30+
fn usage1<T: inh::SliceLike>() {}
31+
}
32+
33+
mod indirect1 {
34+
use rustc_type_ir::inherent::{self}; //~ ERROR non-glob import of `rustc_type_ir::inherent`
35+
use rustc_type_ir::inherent::{self as innate}; //~ ERROR non-glob import of `rustc_type_ir::inherent`
36+
}
37+
38+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
error: non-glob import of `rustc_type_ir::inherent`
2+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:17:9
3+
|
4+
LL | use rustc_type_ir::inherent::Predicate;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^---------
6+
| |
7+
| help: try using a glob import instead: `*`
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:4:9
11+
|
12+
LL | #![deny(rustc::non_glob_import_of_type_ir_inherent)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: non-glob import of `rustc_type_ir::inherent`
16+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:18:35
17+
|
18+
LL | use rustc_type_ir::inherent::{AdtDef, Ty};
19+
| ^^^^^^ help: try using a glob import instead: `*`
20+
21+
error: non-glob import of `rustc_type_ir::inherent`
22+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:18:43
23+
|
24+
LL | use rustc_type_ir::inherent::{AdtDef, Ty};
25+
| ^^ help: try using a glob import instead: `*`
26+
27+
error: non-glob import of `rustc_type_ir::inherent`
28+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:21:9
29+
|
30+
LL | use rustc_type_ir::inherent::ParamEnv as _;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^-------------
32+
| |
33+
| help: try using a glob import instead: `*`
34+
35+
error: non-glob import of `rustc_type_ir::inherent`
36+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:25:9
37+
|
38+
LL | use rustc_type_ir::inherent;
39+
| ^^^^^^^^^^^^^^^^^^^^^^^- help: try using a glob import instead: `::*`
40+
41+
error: non-glob import of `rustc_type_ir::inherent`
42+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:26:9
43+
|
44+
LL | use rustc_type_ir::inherent as inh;
45+
| ^^^^^^^^^^^^^^^^^^^^^^^------- help: try using a glob import instead: `::*`
46+
47+
error: non-glob import of `rustc_type_ir::inherent`
48+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:27:25
49+
|
50+
LL | use rustc_type_ir::{inherent as _};
51+
| ^^^^^^^^----- help: try using a glob import instead: `::*`
52+
53+
error: non-glob import of `rustc_type_ir::inherent`
54+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:34:35
55+
|
56+
LL | use rustc_type_ir::inherent::{self};
57+
| ^^^^ help: try using a glob import instead: `*`
58+
59+
error: non-glob import of `rustc_type_ir::inherent`
60+
--> $DIR/non_glob_import_of_type_ir_inherent.rs:35:35
61+
|
62+
LL | use rustc_type_ir::inherent::{self as innate};
63+
| ^^^^----------
64+
| |
65+
| help: try using a glob import instead: `*`
66+
67+
error: aborting due to 9 previous errors
68+

0 commit comments

Comments
 (0)