Skip to content

Commit ca055bd

Browse files
committed
Introduce Boolean type PermitVariants for legibility
1 parent 2c08a43 commit ca055bd

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);
@@ -2631,7 +2640,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26312640
hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
26322641
debug!(?maybe_qself, ?path);
26332642
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2634-
self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, false)
2643+
self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, PermitVariants::No)
26352644
}
26362645
&hir::TyKind::OpaqueDef(opaque_ty) => {
26372646
// If this is an RPITIT and we are using the new RPITIT lowering scheme, we
@@ -2694,7 +2703,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26942703
segment,
26952704
hir_ty.hir_id,
26962705
hir_ty.span,
2697-
false,
2706+
PermitVariants::No,
26982707
)
26992708
.map(|(ty, _, _)| ty)
27002709
.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
@@ -10,7 +10,7 @@ use rustc_hir::intravisit::Visitor;
1010
use rustc_hir::{ExprKind, HirId, Node, QPath};
1111
use rustc_hir_analysis::check::intrinsicck::InlineAsmCtxt;
1212
use rustc_hir_analysis::check::potentially_plural_count;
13-
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
13+
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, PermitVariants};
1414
use rustc_index::IndexVec;
1515
use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TypeTrace};
1616
use rustc_middle::ty::adjustment::AllowTwoPhase;
@@ -2165,7 +2165,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21652165
match *qpath {
21662166
QPath::Resolved(ref maybe_qself, path) => {
21672167
let self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself).raw);
2168-
let ty = self.lowerer().lower_resolved_ty_path(self_ty, path, hir_id, true);
2168+
let ty = self.lowerer().lower_resolved_ty_path(
2169+
self_ty,
2170+
path,
2171+
hir_id,
2172+
PermitVariants::Yes,
2173+
);
21692174
(path.res, LoweredTy::from_raw(self, path_span, ty))
21702175
}
21712176
QPath::TypeRelative(hir_self_ty, segment) => {
@@ -2177,7 +2182,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21772182
segment,
21782183
hir_id,
21792184
path_span,
2180-
true,
2185+
PermitVariants::Yes,
21812186
);
21822187
let ty = result
21832188
.map(|(ty, _, _)| ty)

0 commit comments

Comments
 (0)