From 91854453f233c2b7084e665c2cf36f5494b26b76 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 28 Aug 2024 00:14:41 -0400 Subject: [PATCH] Deny imports of rustc_type_ir::inherent outside of type ir + new trait solver --- compiler/rustc_lint/messages.ftl | 3 ++ compiler/rustc_lint/src/internal.rs | 30 ++++++++++++++++-- compiler/rustc_lint/src/lints.rs | 5 +++ compiler/rustc_next_trait_solver/src/lib.rs | 1 + compiler/rustc_type_ir/src/lib.rs | 1 + .../import-of-type-ir-inherent.rs | 18 +++++++++++ .../import-of-type-ir-inherent.stderr | 31 +++++++++++++++++++ 7 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.rs create mode 100644 tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.stderr diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 31b7eb5ee7d..35334595833 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -783,6 +783,9 @@ lint_tykind = usage of `ty::TyKind` lint_tykind_kind = usage of `ty::TyKind::` .suggestion = try using `ty::` directly +lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver + .note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler + lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing .label = argument has type `{$arg_ty}` .suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index 65571815019..2e8116b8ba8 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -18,7 +18,7 @@ use crate::lints::{ BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword, NonGlobImportTypeIrInherent, QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, - TykindKind, UntranslatableDiag, + TykindKind, TypeIrInherentUsage, UntranslatableDiag, }; use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; @@ -277,13 +277,39 @@ fn gen_args(segment: &PathSegment<'_>) -> String { report_in_external_macro: true } -declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT]); +declare_tool_lint! { + /// The `usage_of_type_ir_inherent` lint detects usage `rustc_type_ir::inherent`. + /// + /// This module should only be used within the trait solver. + pub rustc::USAGE_OF_TYPE_IR_INHERENT, + Allow, + "usage `rustc_type_ir::inherent` outside of trait system", + report_in_external_macro: true +} + +declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT]); impl<'tcx> LateLintPass<'tcx> for TypeIr { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return }; let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id); + + // Path segments except for the final. + if let Some(seg) = + path.segments.iter().find(|seg| seg.res.opt_def_id().is_some_and(is_mod_inherent)) + { + cx.emit_span_lint(USAGE_OF_TYPE_IR_INHERENT, seg.ident.span, TypeIrInherentUsage); + } + // Final path resolutions, like `use rustc_type_ir::inherent` + else if path.res.iter().any(|res| res.opt_def_id().is_some_and(is_mod_inherent)) { + cx.emit_span_lint( + USAGE_OF_TYPE_IR_INHERENT, + path.segments.last().unwrap().ident.span, + TypeIrInherentUsage, + ); + } + let (lo, hi, snippet) = match path.segments { [.., penultimate, segment] if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) => diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 2712e25668a..7ca282b7c85 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -918,6 +918,11 @@ pub(crate) struct TyQualified { pub suggestion: Span, } +#[derive(LintDiagnostic)] +#[diag(lint_type_ir_inherent_usage)] +#[note] +pub(crate) struct TypeIrInherentUsage; + #[derive(LintDiagnostic)] #[diag(lint_non_glob_import_type_ir_inherent)] pub(crate) struct NonGlobImportTypeIrInherent { diff --git a/compiler/rustc_next_trait_solver/src/lib.rs b/compiler/rustc_next_trait_solver/src/lib.rs index ea244b5107a..ca140500e2c 100644 --- a/compiler/rustc_next_trait_solver/src/lib.rs +++ b/compiler/rustc_next_trait_solver/src/lib.rs @@ -5,6 +5,7 @@ //! So if you got to this crate from the old solver, it's totally normal. // tidy-alphabetical-start +#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_inherent))] #![warn(unreachable_pub)] // tidy-alphabetical-end diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index de41d2f3cc5..f763153a5fc 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -5,6 +5,7 @@ feature(associated_type_defaults, never_type, rustc_attrs, negative_impls) )] #![cfg_attr(feature = "nightly", allow(internal_features))] +#![cfg_attr(not(bootstrap), allow(rustc::usage_of_type_ir_inherent))] // tidy-alphabetical-end extern crate self as rustc_type_ir; diff --git a/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.rs b/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.rs new file mode 100644 index 00000000000..08d86606a6b --- /dev/null +++ b/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Z unstable-options + +// #[cfg(bootstrap)]: We can stop ignoring next beta bump; afterward this ALWAYS should run. +//@ ignore-stage1 + +#![feature(rustc_private)] +#![deny(rustc::usage_of_type_ir_inherent)] + +extern crate rustc_type_ir; + +use rustc_type_ir::inherent::*; +//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver +use rustc_type_ir::inherent; +//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver +use rustc_type_ir::inherent::Predicate; +//~^ ERROR do not use `rustc_type_ir::inherent` unless you're inside of the trait solver + +fn main() {} diff --git a/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.stderr b/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.stderr new file mode 100644 index 00000000000..cc6cb9170c0 --- /dev/null +++ b/tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.stderr @@ -0,0 +1,31 @@ +error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver + --> $DIR/import-of-type-ir-inherent.rs:11:20 + | +LL | use rustc_type_ir::inherent::*; + | ^^^^^^^^ + | + = note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler +note: the lint level is defined here + --> $DIR/import-of-type-ir-inherent.rs:7:9 + | +LL | #![deny(rustc::usage_of_type_ir_inherent)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver + --> $DIR/import-of-type-ir-inherent.rs:13:20 + | +LL | use rustc_type_ir::inherent; + | ^^^^^^^^ + | + = note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler + +error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver + --> $DIR/import-of-type-ir-inherent.rs:15:20 + | +LL | use rustc_type_ir::inherent::Predicate; + | ^^^^^^^^ + | + = note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler + +error: aborting due to 3 previous errors +