Skip to content

Commit bda903e

Browse files
committed
Introduce Boolean type PermitVariants for legibility
1 parent 3fd047d commit bda903e

File tree

2 files changed

+30
-16
lines changed
  • compiler
    • rustc_hir_analysis/src/hir_ty_lowering
    • rustc_hir_typeck/src/fn_ctxt

2 files changed

+30
-16
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,35 +245,42 @@ pub enum FeedConstTy<'a, 'tcx> {
245245

246246
#[derive(Debug, Clone, Copy)]
247247
enum LowerTypeRelativePathMode {
248-
Type { permit_variants: bool },
248+
Type(PermitVariants),
249249
Const,
250250
}
251251

252252
impl LowerTypeRelativePathMode {
253253
fn assoc_tag(self) -> ty::AssocTag {
254254
match self {
255-
Self::Type { .. } => ty::AssocTag::Type,
255+
Self::Type(_) => ty::AssocTag::Type,
256256
Self::Const => ty::AssocTag::Const,
257257
}
258258
}
259259

260260
fn def_kind(self) -> DefKind {
261261
match self {
262-
Self::Type { .. } => DefKind::AssocTy,
262+
Self::Type(_) => DefKind::AssocTy,
263263
Self::Const => DefKind::AssocConst,
264264
}
265265
}
266266

267-
fn permit_variants(self) -> bool {
267+
fn permit_variants(self) -> PermitVariants {
268268
match self {
269-
Self::Type { permit_variants } => permit_variants,
269+
Self::Type(permit_variants) => permit_variants,
270270
// FIXME(mgca): Support paths like `Option::<T>::None` or `Option::<T>::Some` which
271271
// resolve to const ctors/fn items respectively.
272-
Self::Const => false,
272+
Self::Const => PermitVariants::No,
273273
}
274274
}
275275
}
276276

277+
/// Whether to permit a path to resolve to an enum variant.
278+
#[derive(Debug, Clone, Copy)]
279+
pub enum PermitVariants {
280+
Yes,
281+
No,
282+
}
283+
277284
#[derive(Debug, Clone, Copy)]
278285
enum TypeRelativePath<'tcx> {
279286
AssocItem(DefId, GenericArgsRef<'tcx>),
@@ -1160,7 +1167,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11601167
segment: &'tcx hir::PathSegment<'tcx>,
11611168
qpath_hir_id: HirId,
11621169
span: Span,
1163-
permit_variants: bool,
1170+
permit_variants: PermitVariants,
11641171
) -> Result<(Ty<'tcx>, DefKind, DefId), ErrorGuaranteed> {
11651172
let tcx = self.tcx();
11661173
match self.lower_type_relative_path(
@@ -1169,7 +1176,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11691176
segment,
11701177
qpath_hir_id,
11711178
span,
1172-
LowerTypeRelativePathMode::Type { permit_variants },
1179+
LowerTypeRelativePathMode::Type(permit_variants),
11731180
)? {
11741181
TypeRelativePath::AssocItem(def_id, args) => {
11751182
let alias_ty = ty::AliasTy::new_from_args(tcx, def_id, args);
@@ -1245,7 +1252,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12451252
.iter()
12461253
.find(|vd| tcx.hygienic_eq(ident, vd.ident(tcx), adt_def.did()));
12471254
if let Some(variant_def) = variant_def {
1248-
if mode.permit_variants() {
1255+
if let PermitVariants::Yes = mode.permit_variants() {
12491256
tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
12501257
let _ = self.prohibit_generic_args(
12511258
slice::from_ref(segment).iter(),
@@ -2072,7 +2079,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20722079
opt_self_ty: Option<Ty<'tcx>>,
20732080
path: &hir::Path<'tcx>,
20742081
hir_id: HirId,
2075-
permit_variants: bool,
2082+
permit_variants: PermitVariants,
20762083
) -> Ty<'tcx> {
20772084
debug!(?path.res, ?opt_self_ty, ?path.segments);
20782085
let tcx = self.tcx();
@@ -2103,7 +2110,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21032110
);
21042111
self.lower_path_segment(span, did, path.segments.last().unwrap())
21052112
}
2106-
Res::Def(kind @ DefKind::Variant, def_id) if permit_variants => {
2113+
Res::Def(kind @ DefKind::Variant, def_id)
2114+
if let PermitVariants::Yes = permit_variants =>
2115+
{
21072116
// Lower "variant type" as if it were a real type.
21082117
// The resulting `Ty` is type of the variant's enum for now.
21092118
assert_eq!(opt_self_ty, None);
@@ -2633,7 +2642,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26332642
hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
26342643
debug!(?maybe_qself, ?path);
26352644
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2636-
self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, false)
2645+
self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, PermitVariants::No)
26372646
}
26382647
&hir::TyKind::OpaqueDef(opaque_ty) => {
26392648
// If this is an RPITIT and we are using the new RPITIT lowering scheme, we
@@ -2696,7 +2705,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26962705
segment,
26972706
hir_ty.hir_id,
26982707
hir_ty.span,
2699-
false,
2708+
PermitVariants::No,
27002709
)
27012710
.map(|(ty, _, _)| ty)
27022711
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::def_id::DefId;
99
use rustc_hir::intravisit::Visitor;
1010
use rustc_hir::{ExprKind, HirId, Node, QPath};
1111
use rustc_hir_analysis::check::potentially_plural_count;
12-
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
12+
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, PermitVariants};
1313
use rustc_index::IndexVec;
1414
use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TypeTrace};
1515
use rustc_middle::ty::adjustment::AllowTwoPhase;
@@ -2105,7 +2105,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21052105
match *qpath {
21062106
QPath::Resolved(ref maybe_qself, path) => {
21072107
let self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself).raw);
2108-
let ty = self.lowerer().lower_resolved_ty_path(self_ty, path, hir_id, true);
2108+
let ty = self.lowerer().lower_resolved_ty_path(
2109+
self_ty,
2110+
path,
2111+
hir_id,
2112+
PermitVariants::Yes,
2113+
);
21092114
(path.res, LoweredTy::from_raw(self, path_span, ty))
21102115
}
21112116
QPath::TypeRelative(hir_self_ty, segment) => {
@@ -2117,7 +2122,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21172122
segment,
21182123
hir_id,
21192124
path_span,
2120-
true,
2125+
PermitVariants::Yes,
21212126
);
21222127
let ty = result
21232128
.map(|(ty, _, _)| ty)

0 commit comments

Comments
 (0)