Skip to content

Commit 96ab10c

Browse files
authored
Rollup merge of #139313 - oli-obk:push-uzvmpxqyvrzp, r=compiler-errors
Deduplicate some `rustc_middle` function bodies by calling the `rustc_type_ir` equivalent Maybe in the future we can use method delegation, but I'd rather avoid that for now (I don't even know if it can do that already)
2 parents f701a5c + 6189594 commit 96ab10c

File tree

3 files changed

+67
-99
lines changed

3 files changed

+67
-99
lines changed

compiler/rustc_middle/src/ty/sty.rs

+3-44
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::iter;
88
use std::ops::{ControlFlow, Range};
99

1010
use hir::def::{CtorKind, DefKind};
11-
use rustc_abi::{ExternAbi, FIRST_VARIANT, FieldIdx, VariantIdx};
11+
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
1212
use rustc_errors::{ErrorGuaranteed, MultiSpan};
1313
use rustc_hir as hir;
1414
use rustc_hir::LangItem;
@@ -1441,23 +1441,7 @@ impl<'tcx> Ty<'tcx> {
14411441

14421442
#[tracing::instrument(level = "trace", skip(tcx))]
14431443
pub fn fn_sig(self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> {
1444-
match self.kind() {
1445-
FnDef(def_id, args) => tcx.fn_sig(*def_id).instantiate(tcx, args),
1446-
FnPtr(sig_tys, hdr) => sig_tys.with(*hdr),
1447-
Error(_) => {
1448-
// ignore errors (#54954)
1449-
Binder::dummy(ty::FnSig {
1450-
inputs_and_output: ty::List::empty(),
1451-
c_variadic: false,
1452-
safety: hir::Safety::Safe,
1453-
abi: ExternAbi::Rust,
1454-
})
1455-
}
1456-
Closure(..) => bug!(
1457-
"to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",
1458-
),
1459-
_ => bug!("Ty::fn_sig() called on non-fn type: {:?}", self),
1460-
}
1444+
self.kind().fn_sig(tcx)
14611445
}
14621446

14631447
#[inline]
@@ -2043,32 +2027,7 @@ impl<'tcx> Ty<'tcx> {
20432027
/// nested types may be further simplified, the outermost [`TyKind`] or
20442028
/// type constructor remains the same.
20452029
pub fn is_known_rigid(self) -> bool {
2046-
match self.kind() {
2047-
Bool
2048-
| Char
2049-
| Int(_)
2050-
| Uint(_)
2051-
| Float(_)
2052-
| Adt(_, _)
2053-
| Foreign(_)
2054-
| Str
2055-
| Array(_, _)
2056-
| Pat(_, _)
2057-
| Slice(_)
2058-
| RawPtr(_, _)
2059-
| Ref(_, _, _)
2060-
| FnDef(_, _)
2061-
| FnPtr(..)
2062-
| Dynamic(_, _, _)
2063-
| Closure(_, _)
2064-
| CoroutineClosure(_, _)
2065-
| Coroutine(_, _)
2066-
| CoroutineWitness(..)
2067-
| Never
2068-
| Tuple(_)
2069-
| UnsafeBinder(_) => true,
2070-
Error(_) | Infer(_) | Alias(_, _) | Param(_) | Bound(_, _) | Placeholder(_) => false,
2071-
}
2030+
self.kind().is_known_rigid()
20722031
}
20732032
}
20742033

compiler/rustc_type_ir/src/inherent.rs

+2-55
Original file line numberDiff line numberDiff line change
@@ -146,67 +146,14 @@ pub trait Ty<I: Interner<Ty = Self>>:
146146
fn has_unsafe_fields(self) -> bool;
147147

148148
fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
149-
match self.kind() {
150-
ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),
151-
ty::FnDef(def_id, args) => interner.fn_sig(def_id).instantiate(interner, args),
152-
ty::Error(_) => {
153-
// ignore errors (#54954)
154-
ty::Binder::dummy(ty::FnSig {
155-
inputs_and_output: Default::default(),
156-
c_variadic: false,
157-
safety: I::Safety::safe(),
158-
abi: I::Abi::rust(),
159-
})
160-
}
161-
ty::Closure(..) => panic!(
162-
"to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",
163-
),
164-
_ => panic!("Ty::fn_sig() called on non-fn type: {:?}", self),
165-
}
149+
self.kind().fn_sig(interner)
166150
}
167151

168152
fn discriminant_ty(self, interner: I) -> I::Ty;
169153

170154
fn async_destructor_ty(self, interner: I) -> I::Ty;
171-
172-
/// Returns `true` when the outermost type cannot be further normalized,
173-
/// resolved, or instantiated. This includes all primitive types, but also
174-
/// things like ADTs and trait objects, since even if their arguments or
175-
/// nested types may be further simplified, the outermost [`ty::TyKind`] or
176-
/// type constructor remains the same.
177155
fn is_known_rigid(self) -> bool {
178-
match self.kind() {
179-
ty::Bool
180-
| ty::Char
181-
| ty::Int(_)
182-
| ty::Uint(_)
183-
| ty::Float(_)
184-
| ty::Adt(_, _)
185-
| ty::Foreign(_)
186-
| ty::Str
187-
| ty::Array(_, _)
188-
| ty::Pat(_, _)
189-
| ty::Slice(_)
190-
| ty::RawPtr(_, _)
191-
| ty::Ref(_, _, _)
192-
| ty::FnDef(_, _)
193-
| ty::FnPtr(..)
194-
| ty::UnsafeBinder(_)
195-
| ty::Dynamic(_, _, _)
196-
| ty::Closure(_, _)
197-
| ty::CoroutineClosure(_, _)
198-
| ty::Coroutine(_, _)
199-
| ty::CoroutineWitness(..)
200-
| ty::Never
201-
| ty::Tuple(_) => true,
202-
203-
ty::Error(_)
204-
| ty::Infer(_)
205-
| ty::Alias(_, _)
206-
| ty::Param(_)
207-
| ty::Bound(_, _)
208-
| ty::Placeholder(_) => false,
209-
}
156+
self.kind().is_known_rigid()
210157
}
211158
}
212159

compiler/rustc_type_ir/src/ty_kind.rs

+62
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,68 @@ pub enum TyKind<I: Interner> {
273273
Error(I::ErrorGuaranteed),
274274
}
275275

276+
impl<I: Interner> TyKind<I> {
277+
pub fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
278+
match self {
279+
ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),
280+
ty::FnDef(def_id, args) => interner.fn_sig(def_id).instantiate(interner, args),
281+
ty::Error(_) => {
282+
// ignore errors (#54954)
283+
ty::Binder::dummy(ty::FnSig {
284+
inputs_and_output: Default::default(),
285+
c_variadic: false,
286+
safety: I::Safety::safe(),
287+
abi: I::Abi::rust(),
288+
})
289+
}
290+
ty::Closure(..) => panic!(
291+
"to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",
292+
),
293+
_ => panic!("Ty::fn_sig() called on non-fn type: {:?}", self),
294+
}
295+
}
296+
297+
/// Returns `true` when the outermost type cannot be further normalized,
298+
/// resolved, or instantiated. This includes all primitive types, but also
299+
/// things like ADTs and trait objects, since even if their arguments or
300+
/// nested types may be further simplified, the outermost [`ty::TyKind`] or
301+
/// type constructor remains the same.
302+
pub fn is_known_rigid(self) -> bool {
303+
match self {
304+
ty::Bool
305+
| ty::Char
306+
| ty::Int(_)
307+
| ty::Uint(_)
308+
| ty::Float(_)
309+
| ty::Adt(_, _)
310+
| ty::Foreign(_)
311+
| ty::Str
312+
| ty::Array(_, _)
313+
| ty::Pat(_, _)
314+
| ty::Slice(_)
315+
| ty::RawPtr(_, _)
316+
| ty::Ref(_, _, _)
317+
| ty::FnDef(_, _)
318+
| ty::FnPtr(..)
319+
| ty::UnsafeBinder(_)
320+
| ty::Dynamic(_, _, _)
321+
| ty::Closure(_, _)
322+
| ty::CoroutineClosure(_, _)
323+
| ty::Coroutine(_, _)
324+
| ty::CoroutineWitness(..)
325+
| ty::Never
326+
| ty::Tuple(_) => true,
327+
328+
ty::Error(_)
329+
| ty::Infer(_)
330+
| ty::Alias(_, _)
331+
| ty::Param(_)
332+
| ty::Bound(_, _)
333+
| ty::Placeholder(_) => false,
334+
}
335+
}
336+
}
337+
276338
// This is manually implemented because a derive would require `I: Debug`
277339
impl<I: Interner> fmt::Debug for TyKind<I> {
278340
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)