Skip to content

Commit e003917

Browse files
authored
Rollup merge of rust-lang#129678 - compiler-errors:type-ir-inherent, r=fmease
Deny imports of `rustc_type_ir::inherent` outside of type ir + new trait solver We shouldn't encourage using `rustc_type_ir::inherent` outside of the new solver[^1], though this can happen by accident due to rust-analyzer, for example. See rust-lang#127537 (comment) for an example in practice. r? fmease [^1]: Unless we go the fully radical approach of always using these inherent methods everywhere in favor of inherent methods, which would be a major overhaul of the compiler, IMO. I don't really want to consider that possibility right now, tho.
2 parents c90991d + 9185445 commit e003917

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

compiler/rustc_lint/messages.ftl

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

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) =>

compiler/rustc_lint/src/lints.rs

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

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

compiler/rustc_next_trait_solver/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! So if you got to this crate from the old solver, it's totally normal.
66
77
// tidy-alphabetical-start
8+
#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_inherent))]
89
#![warn(unreachable_pub)]
910
// tidy-alphabetical-end
1011

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,18 @@
1+
//@ compile-flags: -Z unstable-options
2+
3+
// #[cfg(bootstrap)]: We can stop ignoring next beta bump; afterward this ALWAYS should run.
4+
//@ ignore-stage1
5+
6+
#![feature(rustc_private)]
7+
#![deny(rustc::usage_of_type_ir_inherent)]
8+
9+
extern crate rustc_type_ir;
10+
11+
use rustc_type_ir::inherent::*;
12+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
13+
use rustc_type_ir::inherent;
14+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
15+
use rustc_type_ir::inherent::Predicate;
16+
//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
17+
18+
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:11: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:7: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:13: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:15: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)