Skip to content

Commit a7ed9c1

Browse files
Make everything builtin!
1 parent de81007 commit a7ed9c1

File tree

23 files changed

+344
-443
lines changed

23 files changed

+344
-443
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_infer::infer::TyCtxtInferExt;
88
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
99
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
11+
use rustc_middle::traits::BuiltinImplSource;
1112
use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, InstanceDef, Ty, TyCtxt};
1213
use rustc_middle::ty::{GenericArgKind, GenericArgs};
1314
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
@@ -766,15 +767,15 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
766767
};
767768

768769
match implsrc {
769-
Ok(Some(ImplSource::Param(_, ty::BoundConstness::ConstIfConst))) => {
770+
Ok(Some(ImplSource::Param(ty::BoundConstness::ConstIfConst, _))) => {
770771
debug!(
771772
"const_trait_impl: provided {:?} via where-clause in {:?}",
772773
trait_ref, param_env
773774
);
774775
return;
775776
}
776777
// Closure: Fn{Once|Mut}
777-
Ok(Some(ImplSource::Builtin(_)))
778+
Ok(Some(ImplSource::Builtin(BuiltinImplSource::Misc, _)))
778779
if trait_ref.self_ty().is_closure()
779780
&& tcx.fn_trait_kind_from_def_id(trait_id).is_some() =>
780781
{

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::LangItem;
77
use rustc_infer::infer::TyCtxtInferExt;
88
use rustc_middle::mir;
99
use rustc_middle::mir::*;
10+
use rustc_middle::traits::BuiltinImplSource;
1011
use rustc_middle::ty::{self, AdtDef, GenericArgsRef, Ty};
1112
use rustc_trait_selection::traits::{
1213
self, ImplSource, Obligation, ObligationCause, ObligationCtxt, SelectionContext,
@@ -172,7 +173,8 @@ impl Qualif for NeedsNonConstDrop {
172173

173174
if !matches!(
174175
impl_src,
175-
ImplSource::Builtin(_) | ImplSource::Param(_, ty::BoundConstness::ConstIfConst)
176+
ImplSource::Builtin(BuiltinImplSource::Misc, _)
177+
| ImplSource::Param(ty::BoundConstness::ConstIfConst, _)
176178
) {
177179
// If our const destruct candidate is not ConstDestruct or implied by the param env,
178180
// then it's bad

compiler/rustc_hir_typeck/src/coercion.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
4646
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
4747
use rustc_infer::traits::{Obligation, PredicateObligation};
4848
use rustc_middle::lint::in_external_macro;
49+
use rustc_middle::traits::BuiltinImplSource;
4950
use rustc_middle::ty::adjustment::{
5051
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
5152
};
@@ -687,12 +688,18 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
687688
}
688689

689690
Ok(Some(impl_source)) => {
691+
// Some builtin coercions are still unstable so we detect
692+
// these here and emit a feature error if coercion doesn't fail
693+
// due to another reason.
690694
match impl_source {
691-
traits::ImplSource::TraitUpcasting(..) => {
695+
traits::ImplSource::Builtin(
696+
BuiltinImplSource::TraitUpcasting { .. },
697+
_,
698+
) => {
692699
has_trait_upcasting_coercion =
693700
Some((trait_pred.self_ty(), trait_pred.trait_ref.args.type_at(1)));
694701
}
695-
traits::ImplSource::TupleUnsizing(_) => {
702+
traits::ImplSource::Builtin(BuiltinImplSource::TupleUnsizing, _) => {
696703
has_unsized_tuple_coercion = true;
697704
}
698705
_ => {}

compiler/rustc_middle/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ macro_rules! span_bug {
4343

4444
#[macro_export]
4545
macro_rules! CloneLiftImpls {
46-
($($ty:ty,)+) => {
46+
($($ty:ty),+ $(,)?) => {
4747
$(
4848
impl<'tcx> $crate::ty::Lift<'tcx> for $ty {
4949
type Lifted = Self;
@@ -59,7 +59,7 @@ macro_rules! CloneLiftImpls {
5959
/// allocated data** (i.e., don't need to be folded).
6060
#[macro_export]
6161
macro_rules! TrivialTypeTraversalImpls {
62-
($($ty:ty,)+) => {
62+
($($ty:ty),+ $(,)?) => {
6363
$(
6464
impl<'tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<'tcx>> for $ty {
6565
fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$crate::ty::TyCtxt<'tcx>>>(

compiler/rustc_middle/src/mir/basic_blocks.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ impl<'tcx> graph::WithPredecessors for BasicBlocks<'tcx> {
178178
}
179179
}
180180

181-
TrivialTypeTraversalAndLiftImpls! {
182-
Cache,
183-
}
181+
TrivialTypeTraversalAndLiftImpls! { Cache }
184182

185183
impl<S: Encoder> Encodable<S> for Cache {
186184
#[inline]

compiler/rustc_middle/src/mir/interpret/error.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ impl Into<ErrorGuaranteed> for ReportedErrorInfo {
6666
}
6767
}
6868

69-
TrivialTypeTraversalAndLiftImpls! {
70-
ErrorHandled,
71-
}
69+
TrivialTypeTraversalAndLiftImpls! { ErrorHandled }
7270

7371
pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>;
7472
pub type EvalToConstValueResult<'tcx> = Result<ConstValue<'tcx>, ErrorHandled>;

compiler/rustc_middle/src/mir/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,7 @@ pub enum BindingForm<'tcx> {
706706
RefForGuard,
707707
}
708708

709-
TrivialTypeTraversalAndLiftImpls! {
710-
BindingForm<'tcx>,
711-
}
709+
TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx> }
712710

713711
mod binding_form_impl {
714712
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

compiler/rustc_middle/src/traits/mod.rs

+28-52
Original file line numberDiff line numberDiff line change
@@ -649,46 +649,31 @@ pub enum ImplSource<'tcx, N> {
649649
/// for some type parameter. The `Vec<N>` represents the
650650
/// obligations incurred from normalizing the where-clause (if
651651
/// any).
652-
Param(Vec<N>, ty::BoundConstness),
652+
Param(ty::BoundConstness, Vec<N>),
653653

654-
/// Virtual calls through an object.
655-
Object(ImplSourceObjectData<N>),
656-
657-
/// Successful resolution for a builtin trait.
658-
Builtin(Vec<N>),
659-
660-
// Unsizing a tuple like `(A, B, ..., X)` to `(A, B, ..., Y)` if `X` unsizes to `Y`
661-
TupleUnsizing(Vec<N>),
662-
663-
/// ImplSource for trait upcasting coercion
664-
TraitUpcasting(ImplSourceTraitUpcastingData<N>),
654+
/// Successful resolution for a builtin impl.
655+
Builtin(BuiltinImplSource, Vec<N>),
665656
}
666657

667658
impl<'tcx, N> ImplSource<'tcx, N> {
668659
pub fn nested_obligations(self) -> Vec<N> {
669660
match self {
670661
ImplSource::UserDefined(i) => i.nested,
671-
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => n,
672-
ImplSource::Object(d) => d.nested,
673-
ImplSource::TraitUpcasting(d) => d.nested,
662+
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => n,
674663
}
675664
}
676665

677666
pub fn borrow_nested_obligations(&self) -> &[N] {
678667
match self {
679668
ImplSource::UserDefined(i) => &i.nested,
680-
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => &n,
681-
ImplSource::Object(d) => &d.nested,
682-
ImplSource::TraitUpcasting(d) => &d.nested,
669+
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => &n,
683670
}
684671
}
685672

686673
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
687674
match self {
688675
ImplSource::UserDefined(i) => &mut i.nested,
689-
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => n,
690-
ImplSource::Object(d) => &mut d.nested,
691-
ImplSource::TraitUpcasting(d) => &mut d.nested,
676+
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => n,
692677
}
693678
}
694679

@@ -702,20 +687,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
702687
args: i.args,
703688
nested: i.nested.into_iter().map(f).collect(),
704689
}),
705-
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
706-
ImplSource::Builtin(n) => ImplSource::Builtin(n.into_iter().map(f).collect()),
707-
ImplSource::TupleUnsizing(n) => {
708-
ImplSource::TupleUnsizing(n.into_iter().map(f).collect())
709-
}
710-
ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData {
711-
vtable_base: o.vtable_base,
712-
nested: o.nested.into_iter().map(f).collect(),
713-
}),
714-
ImplSource::TraitUpcasting(d) => {
715-
ImplSource::TraitUpcasting(ImplSourceTraitUpcastingData {
716-
vtable_vptr_slot: d.vtable_vptr_slot,
717-
nested: d.nested.into_iter().map(f).collect(),
718-
})
690+
ImplSource::Param(ct, n) => ImplSource::Param(ct, n.into_iter().map(f).collect()),
691+
ImplSource::Builtin(source, n) => {
692+
ImplSource::Builtin(source, n.into_iter().map(f).collect())
719693
}
720694
}
721695
}
@@ -739,29 +713,31 @@ pub struct ImplSourceUserDefinedData<'tcx, N> {
739713
pub nested: Vec<N>,
740714
}
741715

742-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
743-
#[derive(TypeFoldable, TypeVisitable)]
744-
pub struct ImplSourceTraitUpcastingData<N> {
716+
#[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Debug)]
717+
pub enum BuiltinImplSource {
718+
/// Some builtin impl we don't need to differentiate. This should be used
719+
/// unless more specific information is necessary.
720+
Misc,
721+
/// A builtin impl for trait objects.
722+
///
723+
/// The vtable is formed by concatenating together the method lists of
724+
/// the base object trait and all supertraits, pointers to supertrait vtable will
725+
/// be provided when necessary; this is the start of `upcast_trait_ref`'s methods
726+
/// in that vtable.
727+
Object { vtable_base: usize },
745728
/// The vtable is formed by concatenating together the method lists of
746729
/// the base object trait and all supertraits, pointers to supertrait vtable will
747730
/// be provided when necessary; this is the position of `upcast_trait_ref`'s vtable
748731
/// within that vtable.
749-
pub vtable_vptr_slot: Option<usize>,
750-
751-
pub nested: Vec<N>,
732+
TraitUpcasting { vtable_vptr_slot: Option<usize> },
733+
/// Unsizing a tuple like `(A, B, ..., X)` to `(A, B, ..., Y)` if `X` unsizes to `Y`.
734+
///
735+
/// This needs to be a separate variant as it is still unstable and we need to emit
736+
/// a feature error when using it on stable.
737+
TupleUnsizing,
752738
}
753739

754-
#[derive(PartialEq, Eq, Clone, TyEncodable, TyDecodable, HashStable, Lift)]
755-
#[derive(TypeFoldable, TypeVisitable)]
756-
pub struct ImplSourceObjectData<N> {
757-
/// The vtable is formed by concatenating together the method lists of
758-
/// the base object trait and all supertraits, pointers to supertrait vtable will
759-
/// be provided when necessary; this is the start of `upcast_trait_ref`'s methods
760-
/// in that vtable.
761-
pub vtable_base: usize,
762-
763-
pub nested: Vec<N>,
764-
}
740+
TrivialTypeTraversalAndLiftImpls! { BuiltinImplSource }
765741

766742
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
767743
pub enum ObjectSafetyViolation {

compiler/rustc_middle/src/traits/select.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ impl From<ErrorGuaranteed> for OverflowError {
304304
}
305305
}
306306

307-
TrivialTypeTraversalAndLiftImpls! {
308-
OverflowError,
309-
}
307+
TrivialTypeTraversalAndLiftImpls! { OverflowError }
310308

311309
impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
312310
fn from(overflow_error: OverflowError) -> SelectionError<'tcx> {

compiler/rustc_middle/src/traits/structural_impls.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ use std::fmt;
66

77
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
88
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9-
match *self {
10-
super::ImplSource::UserDefined(ref v) => write!(f, "{:?}", v),
9+
match self {
10+
super::ImplSource::UserDefined(v) => write!(f, "{:?}", v),
1111

12-
super::ImplSource::Builtin(ref d) => write!(f, "{:?}", d),
13-
14-
super::ImplSource::Object(ref d) => write!(f, "{:?}", d),
12+
super::ImplSource::Builtin(source, d) => {
13+
write!(f, "Builtin({source:?}, {d:?})")
14+
}
1515

16-
super::ImplSource::Param(ref n, ct) => {
16+
super::ImplSource::Param(ct, n) => {
1717
write!(f, "ImplSourceParamData({:?}, {:?})", n, ct)
1818
}
19-
20-
super::ImplSource::TupleUnsizing(ref d) => write!(f, "{:?}", d),
21-
22-
super::ImplSource::TraitUpcasting(ref d) => write!(f, "{:?}", d),
2319
}
2420
}
2521
}
@@ -33,23 +29,3 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceUserDefinedData<'tcx,
3329
)
3430
}
3531
}
36-
37-
impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitUpcastingData<N> {
38-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39-
write!(
40-
f,
41-
"ImplSourceTraitUpcastingData(vtable_vptr_slot={:?}, nested={:?})",
42-
self.vtable_vptr_slot, self.nested
43-
)
44-
}
45-
}
46-
47-
impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<N> {
48-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49-
write!(
50-
f,
51-
"ImplSourceObjectData(vtable_base={}, nested={:?})",
52-
self.vtable_base, self.nested
53-
)
54-
}
55-
}

compiler/rustc_middle/src/ty/abstract_const.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ impl From<ErrorGuaranteed> for NotConstEvaluatable {
2727
}
2828
}
2929

30-
TrivialTypeTraversalAndLiftImpls! {
31-
NotConstEvaluatable,
32-
}
30+
TrivialTypeTraversalAndLiftImpls! { NotConstEvaluatable }
3331

3432
pub type BoundAbstractConst<'tcx> = Result<Option<EarlyBinder<ty::Const<'tcx>>>, ErrorGuaranteed>;
3533

compiler/rustc_middle/src/ty/binding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub enum BindingMode {
66
BindByValue(Mutability),
77
}
88

9-
TrivialTypeTraversalAndLiftImpls! { BindingMode, }
9+
TrivialTypeTraversalAndLiftImpls! { BindingMode }
1010

1111
impl BindingMode {
1212
pub fn convert(BindingAnnotation(by_ref, mutbl): BindingAnnotation) -> BindingMode {

0 commit comments

Comments
 (0)