From 4af1e41c2439f7aa75c9e809b59cf2245a6991f7 Mon Sep 17 00:00:00 2001 From: Nia Deckers Date: Fri, 31 Jul 2026 15:31:27 +0000 Subject: [PATCH] Make `#[fundamental]` only apply to the first argument of `Box` * fundamental only on first arg * woo this made error messages nicer * oh the test was misnamed lol * special case only box * rename tests Co-authored-by: Nia Deckers --- .../src/ty/context/impl_interner.rs | 1 + .../rustc_next_trait_solver/src/coherence.rs | 8 +++++- compiler/rustc_type_ir/src/lang_items.rs | 1 + .../impl-foreign-for-box[foreign_local].rs | 27 +++++++++++++++++++ ...impl-foreign-for-box[foreign_local].stderr | 15 +++++++++++ ...pl-foreign-for-fundamental[foreign].stderr | 14 +++++----- ...n[fundemental[foreign]]-for-foreign.stderr | 3 --- 7 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 tests/ui/coherence/impl-foreign-for-box[foreign_local].rs create mode 100644 tests/ui/coherence/impl-foreign-for-box[foreign_local].stderr diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 26bed229f4d5a..2713866d7177e 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -942,6 +942,7 @@ bidirectional_lang_item_map! { // tidy-alphabetical-start DynMetadata, Option, + OwnedBox, Poll, // tidy-alphabetical-end } diff --git a/compiler/rustc_next_trait_solver/src/coherence.rs b/compiler/rustc_next_trait_solver/src/coherence.rs index 22fe960f0790d..e37e69a617bbd 100644 --- a/compiler/rustc_next_trait_solver/src/coherence.rs +++ b/compiler/rustc_next_trait_solver/src/coherence.rs @@ -3,6 +3,7 @@ use std::ops::ControlFlow; use derive_where::derive_where; use rustc_type_ir::inherent::*; +use rustc_type_ir::lang_items::SolverAdtLangItem; use rustc_type_ir::{ self as ty, InferCtxtLike, Interner, Region, TrivialTypeTraversalImpls, TypeVisitable, TypeVisitableExt, TypeVisitor, @@ -407,12 +408,17 @@ where } // For fundamental types, we just look inside of them. + // Certain lang items (currently, `Box`) have special behaviour here + // and so are special cased. ty::Ref(_, ty, _) => ty.visit_with(self), ty::Adt(def, args) => { if self.def_id_is_local(def.def_id()) { ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty)) } else if def.is_fundamental() { - args.visit_with(self) + match self.infcx.cx().as_adt_lang_item(def.def_id()) { + Some(SolverAdtLangItem::OwnedBox) => args.type_at(0).visit_with(self), + Some(..) | None => args.visit_with(self) + } } else { self.found_non_local_ty(ty) } diff --git a/compiler/rustc_type_ir/src/lang_items.rs b/compiler/rustc_type_ir/src/lang_items.rs index 1671128e67a3a..0bc901e07ea96 100644 --- a/compiler/rustc_type_ir/src/lang_items.rs +++ b/compiler/rustc_type_ir/src/lang_items.rs @@ -21,6 +21,7 @@ pub enum SolverAdtLangItem { // tidy-alphabetical-start DynMetadata, Option, + OwnedBox, Poll, // tidy-alphabetical-end } diff --git a/tests/ui/coherence/impl-foreign-for-box[foreign_local].rs b/tests/ui/coherence/impl-foreign-for-box[foreign_local].rs new file mode 100644 index 0000000000000..2a1d4828f4a93 --- /dev/null +++ b/tests/ui/coherence/impl-foreign-for-box[foreign_local].rs @@ -0,0 +1,27 @@ +//@ compile-flags:--crate-name=test +//@ aux-build:coherence_lib.rs + +// Ensure that `Box` in particular isn't fundamental over +// the allocator parameter (but is over T). + +#![feature(allocator_api)] + +extern crate coherence_lib as lib; + +use lib::*; +use std::alloc::{Allocator, AllocError, Layout}; +use std::ptr::NonNull; + +struct Local; + +unsafe impl Allocator for Local { + fn allocate(&self, _layout: Layout) -> Result, AllocError> { + Err(AllocError) + } + unsafe fn deallocate(&self, _ptr: NonNull, _layout: Layout) {} +} + +impl Remote for Box {} + //~^ ERROR: only traits defined in the current crate can be implemented for types defined outside of the crate [E0117] + +fn main() {} diff --git a/tests/ui/coherence/impl-foreign-for-box[foreign_local].stderr b/tests/ui/coherence/impl-foreign-for-box[foreign_local].stderr new file mode 100644 index 0000000000000..26094c7effa9a --- /dev/null +++ b/tests/ui/coherence/impl-foreign-for-box[foreign_local].stderr @@ -0,0 +1,15 @@ +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate + --> $DIR/impl-foreign-for-box[foreign_local].rs:24:1 + | +LL | impl Remote for Box {} + | ^^^^^^^^^^^^^^^^--------------- + | | + | `str` is not defined in the current crate + | + = note: impl doesn't have any local type before any uncovered type parameters + = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules + = note: define and implement a trait or new type instead + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0117`. diff --git a/tests/ui/coherence/impl-foreign-for-fundamental[foreign].stderr b/tests/ui/coherence/impl-foreign-for-fundamental[foreign].stderr index 596f8436567ae..bdd75df6b77fe 100644 --- a/tests/ui/coherence/impl-foreign-for-fundamental[foreign].stderr +++ b/tests/ui/coherence/impl-foreign-for-fundamental[foreign].stderr @@ -2,10 +2,9 @@ error[E0117]: only traits defined in the current crate can be implemented for ty --> $DIR/impl-foreign-for-fundamental[foreign].rs:10:1 | LL | impl Remote for Box { - | ^^^^^------^^^^^-------- - | | | - | | `i32` is not defined in the current crate - | `std::alloc::Global` is not defined in the current crate + | ^^^^^^^^^^^^^^^^-------- + | | + | `i32` is not defined in the current crate | = note: impl doesn't have any local type before any uncovered type parameters = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules @@ -15,10 +14,9 @@ error[E0117]: only traits defined in the current crate can be implemented for ty --> $DIR/impl-foreign-for-fundamental[foreign].rs:14:1 | LL | impl Remote for Box> { - | ^^^^^^^^------^^^^^---------- - | | | - | | `Rc` is not defined in the current crate - | `std::alloc::Global` is not defined in the current crate + | ^^^^^^^^^^^^^^^^^^^---------- + | | + | `Rc` is not defined in the current crate | = note: impl doesn't have any local type before any uncovered type parameters = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules diff --git a/tests/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr b/tests/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr index 91f1886142cf6..af661faa4ea50 100644 --- a/tests/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr +++ b/tests/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr @@ -6,7 +6,6 @@ LL | impl Remote1> for i32 { | | | | | `i32` is not defined in the current crate | `String` is not defined in the current crate - | `std::alloc::Global` is not defined in the current crate | = note: impl doesn't have any local type before any uncovered type parameters = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules @@ -20,7 +19,6 @@ LL | impl Remote1>> for f64 { | | | | | `f64` is not defined in the current crate | `Rc` is not defined in the current crate - | `std::alloc::Global` is not defined in the current crate | = note: impl doesn't have any local type before any uncovered type parameters = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules @@ -34,7 +32,6 @@ LL | impl Remote1>> for f32 { | | | | | `f32` is not defined in the current crate | `Rc` is not defined in the current crate - | `std::alloc::Global` is not defined in the current crate | = note: impl doesn't have any local type before any uncovered type parameters = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules