Skip to content

Commit

Permalink
Rollup merge of rust-lang#129678 - compiler-errors:type-ir-inherent, …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
workingjubilee authored Sep 2, 2024
2 parents 5680d4e + 9185445 commit 8934019
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@ lint_tykind = usage of `ty::TyKind`
lint_tykind_kind = usage of `ty::TyKind::<kind>`
.suggestion = try using `ty::<kind>` 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
Expand Down
30 changes: 28 additions & 2 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tracing::debug;
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};

Expand Down Expand Up @@ -277,13 +277,39 @@ declare_tool_lint! {
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) =>
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_next_trait_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.rs
Original file line number Diff line number Diff line change
@@ -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() {}
31 changes: 31 additions & 0 deletions tests/ui-fulldeps/internal-lints/import-of-type-ir-inherent.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8934019

Please sign in to comment.