Skip to content

Commit f2cb7cc

Browse files
Deny imports of rustc_type_ir::inherent outside of type ir + new trait solver
1 parent 22572d0 commit f2cb7cc

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,9 @@ lint_tykind = usage of `ty::TyKind`
779779
lint_tykind_kind = usage of `ty::TyKind::<kind>`
780780
.suggestion = try using `ty::<kind>` directly
781781
782+
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
783+
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
784+
782785
lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
783786
.label = argument has type `{$arg_ty}`
784787
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value

Diff for: compiler/rustc_lint/src/internal.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tracing::debug;
1818
use crate::lints::{
1919
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
2020
NonGlobImportTypeIrInherent, QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag,
21-
TykindKind, UntranslatableDiag,
21+
TykindKind, TypeIrInherentUsage, UntranslatableDiag,
2222
};
2323
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2424

@@ -277,13 +277,39 @@ declare_tool_lint! {
277277
report_in_external_macro: true
278278
}
279279

280-
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT]);
280+
declare_tool_lint! {
281+
/// The `usage_of_type_ir_inherent` lint detects usage `rustc_type_ir::inherent`.
282+
///
283+
/// This module should only be used within the trait solver.
284+
pub rustc::USAGE_OF_TYPE_IR_INHERENT,
285+
Allow,
286+
"usage `rustc_type_ir::inherent` outside of trait system",
287+
report_in_external_macro: true
288+
}
289+
290+
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]);
281291

282292
impl<'tcx> LateLintPass<'tcx> for TypeIr {
283293
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
284294
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
285295

286296
let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id);
297+
298+
// Path segments except for the final.
299+
if let Some(seg) =
300+
path.segments.iter().find(|seg| seg.res.opt_def_id().is_some_and(is_mod_inherent))
301+
{
302+
cx.emit_span_lint(USAGE_OF_TYPE_IR_INHERENT, seg.ident.span, TypeIrInherentUsage);
303+
}
304+
// Final path resolutions, like `use rustc_type_ir::inherent`
305+
else if path.res.iter().any(|res| res.opt_def_id().is_some_and(is_mod_inherent)) {
306+
cx.emit_span_lint(
307+
USAGE_OF_TYPE_IR_INHERENT,
308+
path.segments.last().unwrap().ident.span,
309+
TypeIrInherentUsage,
310+
);
311+
}
312+
287313
let (lo, hi, snippet) = match path.segments {
288314
[.., penultimate, segment]
289315
if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) =>

Diff for: compiler/rustc_lint/src/lints.rs

+5
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,11 @@ pub struct TyQualified {
918918
pub suggestion: Span,
919919
}
920920

921+
#[derive(LintDiagnostic)]
922+
#[diag(lint_type_ir_inherent_usage)]
923+
#[note]
924+
pub struct TypeIrInherentUsage;
925+
921926
#[derive(LintDiagnostic)]
922927
#[diag(lint_non_glob_import_type_ir_inherent)]
923928
pub struct NonGlobImportTypeIrInherent {

Diff for: compiler/rustc_next_trait_solver/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! but were uplifted in the process of making the new trait solver generic.
55
//! So if you got to this crate from the old solver, it's totally normal.
66
7+
#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_inherent))]
8+
79
pub mod canonicalizer;
810
pub mod coherence;
911
pub mod delegate;

Diff for: compiler/rustc_type_ir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
feature(associated_type_defaults, never_type, rustc_attrs, negative_impls)
66
)]
77
#![cfg_attr(feature = "nightly", allow(internal_features))]
8+
#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_inherent))]
89
// tidy-alphabetical-end
910

1011
extern crate self as rustc_type_ir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -Z unstable-options
2+
#![feature(rustc_private)]
3+
#![deny(rustc::usage_of_type_ir_inherent)]
4+
5+
extern crate rustc_type_ir;
6+
7+
use rustc_type_ir::inherent::*;
8+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
9+
use rustc_type_ir::inherent;
10+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
11+
use rustc_type_ir::inherent::Predicate;
12+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
2+
--> $DIR/import-of-type-ir-inherent.rs:7:20
3+
|
4+
LL | use rustc_type_ir::inherent::*;
5+
| ^^^^^^^^
6+
|
7+
= note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler
8+
note: the lint level is defined here
9+
--> $DIR/import-of-type-ir-inherent.rs:3:9
10+
|
11+
LL | #![deny(rustc::usage_of_type_ir_inherent)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
15+
--> $DIR/import-of-type-ir-inherent.rs:9:20
16+
|
17+
LL | use rustc_type_ir::inherent;
18+
| ^^^^^^^^
19+
|
20+
= note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler
21+
22+
error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
23+
--> $DIR/import-of-type-ir-inherent.rs:11:20
24+
|
25+
LL | use rustc_type_ir::inherent::Predicate;
26+
| ^^^^^^^^
27+
|
28+
= note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler
29+
30+
error: aborting due to 3 previous errors
31+

0 commit comments

Comments
 (0)