Skip to content

Commit a1a3bef

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 6f935a0 commit a1a3bef

File tree

13 files changed

+111
-5
lines changed

13 files changed

+111
-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_infer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![allow(internal_features)]
1717
#![allow(rustc::diagnostic_outside_of_impl)]
1818
#![allow(rustc::untranslatable_diagnostic)]
19+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1920
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2021
#![doc(rust_logo)]
2122
#![feature(assert_matches)]

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ lint_tykind = usage of `ty::TyKind`
812812
lint_tykind_kind = usage of `ty::TyKind::<kind>`
813813
.suggestion = try using `ty::<kind>` directly
814814
815+
lint_type_ir_direct_use = do not use `rustc_type_ir` unless you are implementing type system internals
816+
.note = use `rustc_middle::ty` instead
817+
815818
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
816819
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
817820

compiler/rustc_lint/src/internal.rs

Lines changed: 28 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>) {
@@ -372,6 +382,21 @@ impl<'tcx> LateLintPass<'tcx> for TypeIr {
372382
NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet },
373383
);
374384
}
385+
386+
fn check_path(
387+
&mut self,
388+
cx: &LateContext<'tcx>,
389+
path: &rustc_hir::Path<'tcx>,
390+
_: rustc_hir::HirId,
391+
) {
392+
if let Some(seg) = path.segments.iter().find(|seg| {
393+
seg.res
394+
.opt_def_id()
395+
.is_some_and(|def_id| cx.tcx.is_diagnostic_item(sym::type_ir, def_id))
396+
}) {
397+
cx.emit_span_lint(DIRECT_USE_OF_RUSTC_TYPE_IR, seg.ident.span, TypeIrDirectUse);
398+
}
399+
}
375400
}
376401

377402
declare_tool_lint! {

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ fn register_internals(store: &mut LintStore) {
668668
LintId::of(USAGE_OF_TYPE_IR_TRAITS),
669669
LintId::of(BAD_OPT_ACCESS),
670670
LintId::of(SPAN_USE_EQ_CTXT),
671+
LintId::of(DIRECT_USE_OF_RUSTC_TYPE_IR),
671672
],
672673
);
673674
}

compiler/rustc_lint/src/lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,11 @@ pub(crate) struct TypeIrInherentUsage;
969969
#[note]
970970
pub(crate) struct TypeIrTraitUsage;
971971

972+
#[derive(LintDiagnostic)]
973+
#[diag(lint_type_ir_direct_use)]
974+
#[note]
975+
pub(crate) struct TypeIrDirectUse;
976+
972977
#[derive(LintDiagnostic)]
973978
#[diag(lint_non_glob_import_type_ir_inherent)]
974979
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
#![cfg_attr(not(bootstrap), feature(sized_hierarchy))]
3233
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3334
#![doc(rust_logo)]

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
@@ -2183,6 +2183,7 @@ symbols! {
21832183
type_changing_struct_update,
21842184
type_const,
21852185
type_id,
2186+
type_ir,
21862187
type_ir_infer_ctxt_like,
21872188
type_ir_inherent,
21882189
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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_middle;
8+
extern crate rustc_type_ir;
9+
10+
use rustc_middle::ty::*; // OK, we have to accept rustc_middle::ty::*
11+
12+
// We have to deny direct import of type_ir
13+
use rustc_type_ir::*;
14+
//~^ ERROR: do not use `rustc_type_ir` unless you are implementing type system internals
15+
16+
// We have to deny direct types usages which resolves to type_ir
17+
fn foo<I: rustc_type_ir::Interner>(cx: I, did: I::DefId) {
18+
//~^ ERROR: do not use `rustc_type_ir` unless you are implementing type system internals
19+
}
20+
21+
fn main() {
22+
let _ = rustc_type_ir::InferConst::Fresh(42);
23+
//~^ ERROR: do not use `rustc_type_ir` unless you are implementing type system internals
24+
let _: rustc_type_ir::InferConst;
25+
//~^ ERROR: do not use `rustc_type_ir` unless you are implementing type system internals
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: do not use `rustc_type_ir` unless you are implementing type system internals
2+
--> $DIR/direct-use-of-rustc-type-ir.rs:13: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: do not use `rustc_type_ir` unless you are implementing type system internals
15+
--> $DIR/direct-use-of-rustc-type-ir.rs:17:11
16+
|
17+
LL | fn foo<I: rustc_type_ir::Interner>(cx: I, did: I::DefId) {
18+
| ^^^^^^^^^^^^^
19+
|
20+
= note: use `rustc_middle::ty` instead
21+
22+
error: do not use `rustc_type_ir` unless you are implementing type system internals
23+
--> $DIR/direct-use-of-rustc-type-ir.rs:22:13
24+
|
25+
LL | let _ = rustc_type_ir::InferConst::Fresh(42);
26+
| ^^^^^^^^^^^^^
27+
|
28+
= note: use `rustc_middle::ty` instead
29+
30+
error: do not use `rustc_type_ir` unless you are implementing type system internals
31+
--> $DIR/direct-use-of-rustc-type-ir.rs:24:12
32+
|
33+
LL | let _: rustc_type_ir::InferConst;
34+
| ^^^^^^^^^^^^^
35+
|
36+
= note: use `rustc_middle::ty` instead
37+
38+
error: aborting due to 4 previous errors
39+

0 commit comments

Comments
 (0)