Skip to content

Commit 9fdb411

Browse files
committed
Implement lint against direct uses of rustc_type_ir in compiler crates
1 parent 9c0bcb5 commit 9fdb411

File tree

10 files changed

+68
-2
lines changed

10 files changed

+68
-2
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
@@ -799,6 +799,9 @@ lint_tykind = usage of `ty::TyKind`
799799
lint_tykind_kind = usage of `ty::TyKind::<kind>`
800800
.suggestion = try using `ty::<kind>` directly
801801
802+
lint_type_ir_direct_use = do not use `rustc_type_ir` unless you're inside of the trait solver or rustc_middle
803+
.note = use rustc_middle instead
804+
802805
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
803806
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
804807

compiler/rustc_lint/src/internal.rs

Lines changed: 25 additions & 2 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

@@ -676,3 +676,26 @@ impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
676676
}
677677
}
678678
}
679+
680+
declare_tool_lint! {
681+
/// The `direct_use_of_rustc_type_ir` lint detects usage of `rustc_type_ir`.
682+
///
683+
/// This module should only be used within the trait solver and some desirable
684+
/// crates like rustc_middle.
685+
pub rustc::DIRECT_USE_OF_RUSTC_TYPE_IR,
686+
Allow,
687+
"usage `rustc_type_ir` abstraction outside of trait system",
688+
report_in_external_macro: true
689+
}
690+
declare_lint_pass!(UseOfTypeIr => [DIRECT_USE_OF_RUSTC_TYPE_IR]);
691+
692+
impl<'tcx> LateLintPass<'tcx> for UseOfTypeIr {
693+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx rustc_hir::Item<'tcx>) {
694+
let rustc_hir::ItemKind::Use(path, _) = item.kind else { return };
695+
if let Some(segment) = path.segments.first()
696+
&& segment.ident.to_string() == "rustc_type_ir"
697+
{
698+
cx.emit_span_lint(DIRECT_USE_OF_RUSTC_TYPE_IR, item.span, TypeIrDirectUse);
699+
}
700+
}
701+
}

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ fn register_internals(store: &mut LintStore) {
638638
store.register_late_mod_pass(|_| Box::new(SpanUseEqCtxt));
639639
store.register_lints(&SymbolInternStringLiteral::lint_vec());
640640
store.register_late_mod_pass(|_| Box::new(SymbolInternStringLiteral));
641+
store.register_lints(&UseOfTypeIr::lint_vec());
642+
store.register_late_mod_pass(|_| Box::new(UseOfTypeIr));
641643
// FIXME(davidtwco): deliberately do not include `UNTRANSLATABLE_DIAGNOSTIC` and
642644
// `DIAGNOSTIC_OUTSIDE_OF_IMPL` here because `-Wrustc::internal` is provided to every crate and
643645
// these lints will trigger all of the time - change this once migration to diagnostic structs
@@ -659,6 +661,7 @@ fn register_internals(store: &mut LintStore) {
659661
LintId::of(USAGE_OF_TYPE_IR_TRAITS),
660662
LintId::of(BAD_OPT_ACCESS),
661663
LintId::of(SPAN_USE_EQ_CTXT),
664+
LintId::of(DIRECT_USE_OF_RUSTC_TYPE_IR),
662665
],
663666
);
664667
}

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_type_ir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
feature(associated_type_defaults, never_type, rustc_attrs, negative_impls)
88
)]
99
#![cfg_attr(feature = "nightly", allow(internal_features))]
10+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1011
// tidy-alphabetical-end
1112

1213
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're inside of the trait solver or rustc_middle
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're inside of the trait solver or rustc_middle
2+
--> $DIR/direct-use-of-rustc-type-ir.rs:9:1
3+
|
4+
LL | use rustc_type_ir::*;
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: use rustc_middle 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)