Skip to content

Commit 25b9263

Browse files
committed
Move GenericArgKind::as_{type,const,region} to GenericArg
1 parent 3f15521 commit 25b9263

File tree

5 files changed

+38
-54
lines changed

5 files changed

+38
-54
lines changed

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ fn build_generic_type_param_di_nodes<'ll, 'tcx>(
11811181
let names = get_parameter_names(cx, generics);
11821182
let template_params: SmallVec<_> = iter::zip(substs, names)
11831183
.filter_map(|(kind, name)| {
1184-
kind.unpack().as_type().map(|ty| {
1184+
kind.as_type().map(|ty| {
11851185
let actual_type =
11861186
cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
11871187
let actual_type_di_node = type_di_node(cx, actual_type);

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
461461
let names = get_parameter_names(cx, generics);
462462
iter::zip(substs, names)
463463
.filter_map(|(kind, name)| {
464-
kind.unpack().as_type().map(|ty| {
464+
kind.as_type().map(|ty| {
465465
let actual_type =
466466
cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
467467
let actual_type_metadata = type_di_node(cx, actual_type);

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
250250

251251
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
252252
match param {
253-
Some(param) => self.var_for_def(span, param).unpack().as_type().unwrap(),
253+
Some(param) => self.var_for_def(span, param).as_type().unwrap(),
254254
None => self.next_ty_var(TypeVariableOrigin {
255255
kind: TypeVariableOriginKind::TypeInference,
256256
span,
@@ -265,7 +265,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
265265
span: Span,
266266
) -> Const<'tcx> {
267267
match param {
268-
Some(param) => self.var_for_def(span, param).unpack().as_const().unwrap(),
268+
Some(param) => self.var_for_def(span, param).as_const().unwrap(),
269269
None => self.next_const_var(
270270
ty,
271271
ConstVariableOrigin { kind: ConstVariableOriginKind::ConstInference, span },

Diff for: compiler/rustc_middle/src/ty/subst.rs

+32-48
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,6 @@ impl<'tcx> GenericArgKind<'tcx> {
103103

104104
GenericArg { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData }
105105
}
106-
107-
#[inline]
108-
pub fn as_type(self) -> Option<Ty<'tcx>> {
109-
match self {
110-
GenericArgKind::Type(ty) => Some(ty),
111-
_ => None,
112-
}
113-
}
114-
115-
#[inline]
116-
pub fn as_region(self) -> Option<ty::Region<'tcx>> {
117-
match self {
118-
GenericArgKind::Lifetime(re) => Some(re),
119-
_ => None,
120-
}
121-
}
122-
123-
#[inline]
124-
pub fn as_const(self) -> Option<ty::Const<'tcx>> {
125-
match self {
126-
GenericArgKind::Const(ct) => Some(ct),
127-
_ => None,
128-
}
129-
}
130106
}
131107

132108
impl<'tcx> fmt::Debug for GenericArg<'tcx> {
@@ -204,30 +180,45 @@ impl<'tcx> GenericArg<'tcx> {
204180
}
205181
}
206182

207-
/// Unpack the `GenericArg` as a region when it is known certainly to be a region.
208-
pub fn expect_region(self) -> ty::Region<'tcx> {
183+
#[inline]
184+
pub fn as_type(self) -> Option<Ty<'tcx>> {
209185
match self.unpack() {
210-
GenericArgKind::Lifetime(lt) => lt,
211-
_ => bug!("expected a region, but found another kind"),
186+
GenericArgKind::Type(ty) => Some(ty),
187+
_ => None,
212188
}
213189
}
214190

191+
#[inline]
192+
pub fn as_region(self) -> Option<ty::Region<'tcx>> {
193+
match self.unpack() {
194+
GenericArgKind::Lifetime(re) => Some(re),
195+
_ => None,
196+
}
197+
}
198+
199+
#[inline]
200+
pub fn as_const(self) -> Option<ty::Const<'tcx>> {
201+
match self.unpack() {
202+
GenericArgKind::Const(ct) => Some(ct),
203+
_ => None,
204+
}
205+
}
206+
207+
/// Unpack the `GenericArg` as a region when it is known certainly to be a region.
208+
pub fn expect_region(self) -> ty::Region<'tcx> {
209+
self.as_region().unwrap_or_else(|| bug!("expected a region, but found another kind"))
210+
}
211+
215212
/// Unpack the `GenericArg` as a type when it is known certainly to be a type.
216213
/// This is true in cases where `Substs` is used in places where the kinds are known
217214
/// to be limited (e.g. in tuples, where the only parameters are type parameters).
218215
pub fn expect_ty(self) -> Ty<'tcx> {
219-
match self.unpack() {
220-
GenericArgKind::Type(ty) => ty,
221-
_ => bug!("expected a type, but found another kind"),
222-
}
216+
self.as_type().unwrap_or_else(|| bug!("expected a type, but found another kind"))
223217
}
224218

225219
/// Unpack the `GenericArg` as a const when it is known certainly to be a const.
226220
pub fn expect_const(self) -> ty::Const<'tcx> {
227-
match self.unpack() {
228-
GenericArgKind::Const(c) => c,
229-
_ => bug!("expected a const, but found another kind"),
230-
}
221+
self.as_const().unwrap_or_else(|| bug!("expected a const, but found another kind"))
231222
}
232223

233224
pub fn is_non_region_infer(self) -> bool {
@@ -403,17 +394,17 @@ impl<'tcx> InternalSubsts<'tcx> {
403394

404395
#[inline]
405396
pub fn types(&'tcx self) -> impl DoubleEndedIterator<Item = Ty<'tcx>> + 'tcx {
406-
self.iter().filter_map(|k| k.unpack().as_type())
397+
self.iter().filter_map(|k| k.as_type())
407398
}
408399

409400
#[inline]
410401
pub fn regions(&'tcx self) -> impl DoubleEndedIterator<Item = ty::Region<'tcx>> + 'tcx {
411-
self.iter().filter_map(|k| k.unpack().as_region())
402+
self.iter().filter_map(|k| k.as_region())
412403
}
413404

414405
#[inline]
415406
pub fn consts(&'tcx self) -> impl DoubleEndedIterator<Item = ty::Const<'tcx>> + 'tcx {
416-
self.iter().filter_map(|k| k.unpack().as_const())
407+
self.iter().filter_map(|k| k.as_const())
417408
}
418409

419410
#[inline]
@@ -429,28 +420,21 @@ impl<'tcx> InternalSubsts<'tcx> {
429420
#[inline]
430421
#[track_caller]
431422
pub fn type_at(&self, i: usize) -> Ty<'tcx> {
432-
self[i]
433-
.unpack()
434-
.as_type()
435-
.unwrap_or_else(|| bug!("expected type for param #{} in {:?}", i, self))
423+
self[i].as_type().unwrap_or_else(|| bug!("expected type for param #{} in {:?}", i, self))
436424
}
437425

438426
#[inline]
439427
#[track_caller]
440428
pub fn region_at(&self, i: usize) -> ty::Region<'tcx> {
441429
self[i]
442-
.unpack()
443430
.as_region()
444431
.unwrap_or_else(|| bug!("expected region for param #{} in {:?}", i, self))
445432
}
446433

447434
#[inline]
448435
#[track_caller]
449436
pub fn const_at(&self, i: usize) -> ty::Const<'tcx> {
450-
self[i]
451-
.unpack()
452-
.as_const()
453-
.unwrap_or_else(|| bug!("expected const for param #{} in {:?}", i, self))
437+
self[i].as_const().unwrap_or_else(|| bug!("expected const for param #{} in {:?}", i, self))
454438
}
455439

456440
#[inline]

Diff for: compiler/rustc_mir_transform/src/function_item_references.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'tcx> Visitor<'tcx> for FunctionItemRefChecker<'_, 'tcx> {
4545
// Handle calls to `transmute`
4646
if self.tcx.is_diagnostic_item(sym::transmute, def_id) {
4747
let arg_ty = args[0].ty(self.body, self.tcx);
48-
for inner_ty in arg_ty.walk().filter_map(|arg| arg.unpack().as_type()) {
48+
for inner_ty in arg_ty.walk().filter_map(|arg| arg.as_type()) {
4949
if let Some((fn_id, fn_substs)) =
5050
FunctionItemRefChecker::is_fn_ref(inner_ty)
5151
{
@@ -80,7 +80,7 @@ impl<'tcx> FunctionItemRefChecker<'_, 'tcx> {
8080
let arg_defs = self.tcx.fn_sig(def_id).subst_identity().skip_binder().inputs();
8181
for (arg_num, arg_def) in arg_defs.iter().enumerate() {
8282
// For all types reachable from the argument type in the fn sig
83-
for inner_ty in arg_def.walk().filter_map(|arg| arg.unpack().as_type()) {
83+
for inner_ty in arg_def.walk().filter_map(|arg| arg.as_type()) {
8484
// If the inner type matches the type bound by `Pointer`
8585
if inner_ty == bound_ty {
8686
// Do a substitution using the parameters from the callsite

0 commit comments

Comments
 (0)