Skip to content

Commit ac18786

Browse files
Implement lint against using Interner and InferCtxtLike in random compiler crates
1 parent 0897e3e commit ac18786

File tree

10 files changed

+66
-3
lines changed

10 files changed

+66
-3
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ lint_tykind_kind = usage of `ty::TyKind::<kind>`
799799
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
800800
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
801801
802+
lint_type_ir_trait_usage = do not use `rustc_type_ir::Interner` or `rustc_type_ir::InferCtxtLike` unless you're inside of the trait solver
803+
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
804+
802805
lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
803806
.label = argument has type `{$arg_ty}`
804807
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value

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

+36-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::lints::{
1515
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
1616
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
1717
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
18-
UntranslatableDiag,
18+
TypeIrTraitUsage, UntranslatableDiag,
1919
};
2020
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2121

@@ -280,7 +280,7 @@ declare_tool_lint! {
280280
}
281281

282282
declare_tool_lint! {
283-
/// The `usage_of_type_ir_inherent` lint detects usage `rustc_type_ir::inherent`.
283+
/// The `usage_of_type_ir_inherent` lint detects usage of `rustc_type_ir::inherent`.
284284
///
285285
/// This module should only be used within the trait solver.
286286
pub rustc::USAGE_OF_TYPE_IR_INHERENT,
@@ -289,9 +289,42 @@ declare_tool_lint! {
289289
report_in_external_macro: true
290290
}
291291

292-
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]);
292+
declare_tool_lint! {
293+
/// The `usage_of_type_ir_inherent` lint detects usage of `rustc_type_ir::Interner`,
294+
/// or `rustc_infer::InferCtxtLike`.
295+
///
296+
/// Methods of this trait should only be used within the type system abstraction layer,
297+
/// and in the generic next trait solver implementation. Look for an analogously named
298+
/// method on `TyCtxt` or `InferCtxt` (respectively).
299+
pub rustc::USAGE_OF_TYPE_IR_TRAITS,
300+
Allow,
301+
"usage `rustc_type_ir`-specific abstraction traits outside of trait system",
302+
report_in_external_macro: true
303+
}
304+
305+
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
293306

294307
impl<'tcx> LateLintPass<'tcx> for TypeIr {
308+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
309+
let res_def_id = match expr.kind {
310+
hir::ExprKind::Path(hir::QPath::Resolved(_, path)) => path.res.opt_def_id(),
311+
hir::ExprKind::Path(hir::QPath::TypeRelative(..)) | hir::ExprKind::MethodCall(..) => {
312+
cx.typeck_results().type_dependent_def_id(expr.hir_id)
313+
}
314+
_ => return,
315+
};
316+
let Some(res_def_id) = res_def_id else {
317+
return;
318+
};
319+
if let Some(assoc_item) = cx.tcx.opt_associated_item(res_def_id)
320+
&& let Some(trait_def_id) = assoc_item.trait_container(cx.tcx)
321+
&& (cx.tcx.is_diagnostic_item(sym::type_ir_interner, trait_def_id)
322+
| cx.tcx.is_diagnostic_item(sym::type_ir_infer_ctxt_like, trait_def_id))
323+
{
324+
cx.emit_span_lint(USAGE_OF_TYPE_IR_TRAITS, expr.span, TypeIrTraitUsage);
325+
}
326+
}
327+
295328
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
296329
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
297330

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

+1
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ fn register_internals(store: &mut LintStore) {
645645
LintId::of(USAGE_OF_QUALIFIED_TY),
646646
LintId::of(NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT),
647647
LintId::of(USAGE_OF_TYPE_IR_INHERENT),
648+
LintId::of(USAGE_OF_TYPE_IR_TRAITS),
648649
LintId::of(BAD_OPT_ACCESS),
649650
LintId::of(SPAN_USE_EQ_CTXT),
650651
],

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

+5
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,11 @@ pub(crate) struct TyQualified {
943943
#[note]
944944
pub(crate) struct TypeIrInherentUsage;
945945

946+
#[derive(LintDiagnostic)]
947+
#[diag(lint_type_ir_trait_usage)]
948+
#[note]
949+
pub(crate) struct TypeIrTraitUsage;
950+
946951
#[derive(LintDiagnostic)]
947952
#[diag(lint_non_glob_import_type_ir_inherent)]
948953
pub(crate) struct NonGlobImportTypeIrInherent {

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
// tidy-alphabetical-start
88
#![allow(rustc::usage_of_type_ir_inherent)]
9+
#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_traits))]
910
// tidy-alphabetical-end
1011

1112
pub mod canonicalizer;

Diff for: compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,9 @@ symbols! {
21042104
type_changing_struct_update,
21052105
type_const,
21062106
type_id,
2107+
type_ir_infer_ctxt_like,
21072108
type_ir_inherent,
2109+
type_ir_interner,
21082110
type_length_limit,
21092111
type_macros,
21102112
type_name,

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

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl<I: Interner> TypingMode<I> {
102102
}
103103
}
104104

105+
#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_infer_ctxt_like")]
105106
pub trait InferCtxtLike: Sized {
106107
type Interner: Interner;
107108
fn cx(&self) -> Self::Interner;

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

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::solve::{CanonicalInput, ExternalConstraintsData, PredefinedOpaquesDat
1515
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
1616
use crate::{self as ty, search_graph};
1717

18+
#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_interner")]
1819
pub trait Interner:
1920
Sized
2021
+ Copy

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

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

1112
extern crate self as rustc_type_ir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ compile-flags: -Z unstable-options
2+
3+
#![feature(rustc_private)]
4+
#![deny(rustc::usage_of_type_ir_traits)]
5+
6+
extern crate rustc_type_ir;
7+
8+
use rustc_type_ir::Interner;
9+
//~^ ERROR do not use `rustc_type_ir::Interner` or `rustc_type_ir::InferCtxtLike` unless you're inside of the trait solver
10+
11+
fn foo<I: Interner>(cx: I, did: I::DefId) {
12+
let _ = cx.trait_is_unsafe(did);
13+
}
14+
15+
fn main() {}

0 commit comments

Comments
 (0)