Skip to content

Commit 7c46544

Browse files
authored
Rollup merge of rust-lang#141614 - rperier:lint_type-ir-to-type-middle, r=compiler-errors
lint direct use of rustc_type_ir cc rust-lang#138449 As previously discussed with `@lcnr,` it is a lint to prevent direct use of rustc_type_ir, except for some internal crates (like next_trait_solver or rustc_middle for example).
2 parents 6e2a26e + a1a3bef commit 7c46544

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,

0 commit comments

Comments
 (0)