Skip to content

Commit b408e48

Browse files
committed
Implement lint against direct uses of rustc_type_ir in compiler crates
This commit adds a lint to prevent the use of rustc_type_ir in random compiler crates, except for type system internals traits, which are explicitly allowed. Moreover, this fixes diagnostic_items() to include the CRATE_OWNER_ID, otherwise rustc_diagnostic_item attribute is ignored on the crate root.
1 parent cb678b9 commit b408e48

File tree

12 files changed

+65
-5
lines changed

12 files changed

+65
-5
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![allow(internal_features)]
88
#![allow(rustc::diagnostic_outside_of_impl)]
99
#![allow(rustc::untranslatable_diagnostic)]
10+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1011
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1112
#![doc(rust_logo)]
1213
#![feature(array_windows)]

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ lint_tykind = usage of `ty::TyKind`
801801
lint_tykind_kind = usage of `ty::TyKind::<kind>`
802802
.suggestion = try using `ty::<kind>` directly
803803
804+
lint_type_ir_direct_use = do not use `rustc_type_ir` unless you are implementing type system internals
805+
.note = use `rustc_middle::ty` instead
806+
804807
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
805808
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
806809

compiler/rustc_lint/src/internal.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use {rustc_ast as ast, rustc_hir as hir};
1414
use crate::lints::{
1515
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
1616
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
17-
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
18-
TypeIrTraitUsage, UntranslatableDiag,
17+
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrDirectUse,
18+
TypeIrInherentUsage, TypeIrTraitUsage, UntranslatableDiag,
1919
};
2020
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2121

@@ -301,8 +301,18 @@ declare_tool_lint! {
301301
"usage `rustc_type_ir`-specific abstraction traits outside of trait system",
302302
report_in_external_macro: true
303303
}
304+
declare_tool_lint! {
305+
/// The `direct_use_of_rustc_type_ir` lint detects usage of `rustc_type_ir`.
306+
///
307+
/// This module should only be used within the trait solver and some desirable
308+
/// crates like rustc_middle.
309+
pub rustc::DIRECT_USE_OF_RUSTC_TYPE_IR,
310+
Allow,
311+
"usage `rustc_type_ir` abstraction outside of trait system",
312+
report_in_external_macro: true
313+
}
304314

305-
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
315+
declare_lint_pass!(TypeIr => [DIRECT_USE_OF_RUSTC_TYPE_IR, NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
306316

307317
impl<'tcx> LateLintPass<'tcx> for TypeIr {
308318
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
@@ -344,6 +354,13 @@ impl<'tcx> LateLintPass<'tcx> for TypeIr {
344354
TypeIrInherentUsage,
345355
);
346356
}
357+
if let Some(seg) = path.segments.iter().find(|seg| {
358+
seg.res
359+
.opt_def_id()
360+
.is_some_and(|def_id| cx.tcx.is_diagnostic_item(sym::type_ir, def_id))
361+
}) {
362+
cx.emit_span_lint(DIRECT_USE_OF_RUSTC_TYPE_IR, seg.ident.span, TypeIrDirectUse);
363+
}
347364

348365
let (lo, hi, snippet) = match path.segments {
349366
[.., penultimate, segment]

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ fn register_internals(store: &mut LintStore) {
663663
LintId::of(USAGE_OF_TYPE_IR_TRAITS),
664664
LintId::of(BAD_OPT_ACCESS),
665665
LintId::of(SPAN_USE_EQ_CTXT),
666+
LintId::of(DIRECT_USE_OF_RUSTC_TYPE_IR),
666667
],
667668
);
668669
}

compiler/rustc_lint/src/lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@ pub(crate) struct TypeIrInherentUsage;
995995
#[note]
996996
pub(crate) struct TypeIrTraitUsage;
997997

998+
#[derive(LintDiagnostic)]
999+
#[diag(lint_type_ir_direct_use)]
1000+
#[note]
1001+
pub(crate) struct TypeIrDirectUse;
1002+
9981003
#[derive(LintDiagnostic)]
9991004
#[diag(lint_non_glob_import_type_ir_inherent)]
10001005
pub(crate) struct NonGlobImportTypeIrInherent {

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![allow(internal_features)]
2929
#![allow(rustc::diagnostic_outside_of_impl)]
3030
#![allow(rustc::untranslatable_diagnostic)]
31+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
3132
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3233
#![doc(rust_logo)]
3334
#![feature(allocator_api)]

compiler/rustc_next_trait_solver/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// tidy-alphabetical-start
88
#![allow(rustc::usage_of_type_ir_inherent)]
99
#![allow(rustc::usage_of_type_ir_traits)]
10+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1011
// tidy-alphabetical-end
1112

1213
pub mod canonicalizer;

compiler/rustc_passes/src/diagnostic_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! * Compiler internal types like `Ty` and `TyCtxt`
1111
1212
use rustc_hir::diagnostic_items::DiagnosticItems;
13-
use rustc_hir::{Attribute, OwnerId};
13+
use rustc_hir::{Attribute, CRATE_OWNER_ID, OwnerId};
1414
use rustc_middle::query::{LocalCrate, Providers};
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_span::def_id::{DefId, LOCAL_CRATE};
@@ -67,7 +67,7 @@ fn diagnostic_items(tcx: TyCtxt<'_>, _: LocalCrate) -> DiagnosticItems {
6767

6868
// Collect diagnostic items in this crate.
6969
let crate_items = tcx.hir_crate_items(());
70-
for id in crate_items.owners() {
70+
for id in crate_items.owners().chain(std::iter::once(CRATE_OWNER_ID)) {
7171
observe_item(tcx, &mut diagnostic_items, id);
7272
}
7373

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,7 @@ symbols! {
21522152
type_changing_struct_update,
21532153
type_const,
21542154
type_id,
2155+
type_ir,
21552156
type_ir_infer_ctxt_like,
21562157
type_ir_inherent,
21572158
type_ir_interner,

compiler/rustc_type_ir/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir")]
12
// tidy-alphabetical-start
23
#![allow(rustc::usage_of_ty_tykind)]
34
#![allow(rustc::usage_of_type_ir_inherent)]
@@ -7,6 +8,7 @@
78
feature(associated_type_defaults, never_type, rustc_attrs, negative_impls)
89
)]
910
#![cfg_attr(feature = "nightly", allow(internal_features))]
11+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1012
// tidy-alphabetical-end
1113

1214
extern crate self as rustc_type_ir;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ compile-flags: -Z unstable-options
2+
//@ ignore-stage1
3+
4+
#![feature(rustc_private)]
5+
#![deny(rustc::direct_use_of_rustc_type_ir)]
6+
7+
extern crate rustc_type_ir;
8+
9+
use rustc_type_ir::*;
10+
//~^ ERROR: do not use `rustc_type_ir` unless you are implementing type system internals
11+
12+
13+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: do not use `rustc_type_ir` unless you are implementing type system internals
2+
--> $DIR/direct-use-of-rustc-type-ir.rs:9:5
3+
|
4+
LL | use rustc_type_ir::*;
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: use `rustc_middle::ty` instead
8+
note: the lint level is defined here
9+
--> $DIR/direct-use-of-rustc-type-ir.rs:5:9
10+
|
11+
LL | #![deny(rustc::direct_use_of_rustc_type_ir)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)