Skip to content

Commit f97e9f8

Browse files
committed
make as_ambig_ty/ct explicitly fallible
1 parent 9ed0cd5 commit f97e9f8

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,16 +1110,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11101110

11111111
let ct =
11121112
self.lower_const_path_to_const_arg(path, res, ty.id, ty.span);
1113-
return GenericArg::Const(ct.as_ambig_ct());
1113+
return GenericArg::Const(ct.try_as_ambig_ct().unwrap());
11141114
}
11151115
}
11161116
}
11171117
_ => {}
11181118
}
1119-
GenericArg::Type(self.lower_ty(ty, itctx).as_ambig_ty())
1119+
GenericArg::Type(self.lower_ty(ty, itctx).try_as_ambig_ty().unwrap())
11201120
}
11211121
ast::GenericArg::Const(ct) => {
1122-
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).as_ambig_ct())
1122+
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).try_as_ambig_ct().unwrap())
11231123
}
11241124
}
11251125
}

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
526526
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
527527
};
528528
let args = smallvec![GenericArg::Type(
529-
self.arena.alloc(self.ty_tup(*inputs_span, inputs)).as_ambig_ty()
529+
self.arena.alloc(self.ty_tup(*inputs_span, inputs)).try_as_ambig_ty().unwrap()
530530
)];
531531

532532
// If we have a bound like `async Fn() -> T`, make sure that we mark the

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,16 @@ impl<'hir> ConstArg<'hir, AmbigArg> {
280280
}
281281

282282
impl<'hir> ConstArg<'hir> {
283-
pub fn as_ambig_ct(&self) -> &ConstArg<'hir, AmbigArg> {
284-
assert!(!matches!(self.kind, ConstArgKind::Infer(_, ())));
283+
pub fn try_as_ambig_ct(&self) -> Option<&ConstArg<'hir, AmbigArg>> {
284+
if let ConstArgKind::Infer(_, ()) = self.kind {
285+
return None;
286+
}
285287

286288
// SAFETY: `ConstArg` is `repr(C)` and `ConstArgKind` is marked `repr(u8)` so that the layout is
287289
// the same across different ZST type arguments. We also asserted that the `self` is
288290
// not a `ConstArgKind::Infer` so there is no risk of transmuting a `()` to `AmbigArg`.
289291
let ptr = self as *const ConstArg<'hir> as *const ConstArg<'hir, AmbigArg>;
290-
unsafe { &*ptr }
292+
Some(unsafe { &*ptr })
291293
}
292294
}
293295

@@ -2912,14 +2914,16 @@ impl<'hir> Ty<'hir, AmbigArg> {
29122914
}
29132915

29142916
impl<'hir> Ty<'hir> {
2915-
pub fn as_ambig_ty(&self) -> &Ty<'hir, AmbigArg> {
2916-
assert!(!matches!(self.kind, TyKind::Infer(())));
2917+
pub fn try_as_ambig_ty(&self) -> Option<&Ty<'hir, AmbigArg>> {
2918+
if let TyKind::Infer(()) = self.kind {
2919+
return None;
2920+
}
29172921

29182922
// SAFETY: `Ty` is `repr(C)` and `TyKind` is marked `repr(u8)` so that the layout is
29192923
// the same across different ZST type arguments. We also asserted that the `self` is
29202924
// not a `TyKind::Infer` so there is no risk of transmuting a `()` to `AmbigArg`.
29212925
let ptr = self as *const Ty<'hir> as *const Ty<'hir, AmbigArg>;
2922-
unsafe { &*ptr }
2926+
Some(unsafe { &*ptr })
29232927
}
29242928
}
29252929

compiler/rustc_hir/src/intravisit.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,13 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(
901901
}
902902

903903
pub fn walk_unambig_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Result {
904-
if let TyKind::Infer(()) = typ.kind {
905-
try_visit!(visitor.visit_id(typ.hir_id));
906-
return visitor.visit_shared_ty_const(typ.hir_id, typ.span, InferKind::Ty(typ));
904+
match typ.try_as_ambig_ty() {
905+
Some(ambig_ty) => visitor.visit_ty(ambig_ty),
906+
None => {
907+
try_visit!(visitor.visit_id(typ.hir_id));
908+
visitor.visit_shared_ty_const(typ.hir_id, typ.span, InferKind::Ty(typ))
909+
}
907910
}
908-
909-
visitor.visit_ty(typ.as_ambig_ty())
910911
}
911912

912913
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v, AmbigArg>) -> V::Result {
@@ -964,12 +965,17 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
964965
visitor: &mut V,
965966
const_arg: &'v ConstArg<'v>,
966967
) -> V::Result {
967-
if let ConstArgKind::Infer(sp, ()) = const_arg.kind {
968-
try_visit!(visitor.visit_id(const_arg.hir_id));
969-
return visitor.visit_shared_ty_const(const_arg.hir_id, sp, InferKind::Const(const_arg));
968+
match const_arg.try_as_ambig_ct() {
969+
Some(ambig_ct) => visitor.visit_const_arg(ambig_ct),
970+
None => {
971+
try_visit!(visitor.visit_id(const_arg.hir_id));
972+
visitor.visit_shared_ty_const(
973+
const_arg.hir_id,
974+
const_arg.span(),
975+
InferKind::Const(const_arg),
976+
)
977+
}
970978
}
971-
972-
visitor.visit_const_arg(const_arg.as_ambig_ct())
973979
}
974980

975981
pub fn walk_ambig_const_arg<'v, V: Visitor<'v>>(

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_errors::ErrorGuaranteed;
44
use rustc_hir::def::{Namespace, Res};
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::intravisit::Visitor;
6+
use rustc_hir::intravisit::{Visitor, walk_ty};
77
use rustc_hir::{self as hir, AmbigArg};
88
use rustc_middle::hir::nested_filter;
99
use rustc_middle::traits::ObligationCauseCode;
@@ -141,9 +141,8 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
141141
match arg.kind {
142142
hir::TyKind::Ref(_, ref mut_ty) => {
143143
// We don't want to suggest looking into borrowing `&T` or `&Self`.
144-
match mut_ty.ty.kind {
145-
hir::TyKind::Infer(()) => (),
146-
_ => hir::intravisit::walk_ty(self, mut_ty.ty.as_ambig_ty()),
144+
if let Some(ambig_ty) = mut_ty.ty.try_as_ambig_ty() {
145+
walk_ty(self, ambig_ty);
147146
}
148147
return;
149148
}

0 commit comments

Comments
 (0)