Skip to content

Commit 0349f8b

Browse files
committed
Use a yes/no enum instead of a bool.
The bool's meaning wasn't obvious to me at some call sites.
1 parent 5428983 commit 0349f8b

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

compiler/rustc_resolve/src/ident.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ impl<'a> Resolver<'a> {
11791179
ConstantItemRibKind(trivial, _) => {
11801180
let features = self.session.features_untracked();
11811181
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1182-
if !(trivial || features.generic_const_exprs) {
1182+
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
11831183
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
11841184
// we can't easily tell if it's generic at this stage, so we instead remember
11851185
// this and then enforce the self type to be concrete later on.
@@ -1267,7 +1267,7 @@ impl<'a> Resolver<'a> {
12671267
ConstantItemRibKind(trivial, _) => {
12681268
let features = self.session.features_untracked();
12691269
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1270-
if !(trivial || features.generic_const_exprs) {
1270+
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
12711271
if let Some(span) = finalize {
12721272
self.report_error(
12731273
span,

compiler/rustc_resolve/src/late.rs

+48-26
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ crate enum HasGenericParams {
9494
No,
9595
}
9696

97+
impl HasGenericParams {
98+
fn force_yes_if(self, b: bool) -> Self {
99+
if b { Self::Yes } else { self }
100+
}
101+
}
102+
97103
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
98104
crate enum ConstantItemKind {
99105
Const,
@@ -125,9 +131,9 @@ crate enum RibKind<'a> {
125131

126132
/// We're in a constant item. Can't refer to dynamic stuff.
127133
///
128-
/// The `bool` indicates if this constant may reference generic parameters
129-
/// and is used to only allow generic parameters to be used in trivial constant expressions.
130-
ConstantItemRibKind(bool, Option<(Ident, ConstantItemKind)>),
134+
/// The item may reference generic parameters in trivial constant expressions.
135+
/// All other constants aren't allowed to use generic params at all.
136+
ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>),
131137

132138
/// We passed through a module.
133139
ModuleRibKind(Module<'a>),
@@ -826,19 +832,24 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
826832
// Note that we might not be inside of an repeat expression here,
827833
// but considering that `IsRepeatExpr` is only relevant for
828834
// non-trivial constants this is doesn't matter.
829-
self.with_constant_rib(IsRepeatExpr::No, true, None, |this| {
830-
this.smart_resolve_path(
831-
ty.id,
832-
qself.as_ref(),
833-
path,
834-
PathSource::Expr(None),
835-
);
836-
837-
if let Some(ref qself) = *qself {
838-
this.visit_ty(&qself.ty);
839-
}
840-
this.visit_path(path, ty.id);
841-
});
835+
self.with_constant_rib(
836+
IsRepeatExpr::No,
837+
HasGenericParams::Yes,
838+
None,
839+
|this| {
840+
this.smart_resolve_path(
841+
ty.id,
842+
qself.as_ref(),
843+
path,
844+
PathSource::Expr(None),
845+
);
846+
847+
if let Some(ref qself) = *qself {
848+
this.visit_ty(&qself.ty);
849+
}
850+
this.visit_path(path, ty.id);
851+
},
852+
);
842853

843854
self.diagnostic_metadata.currently_processing_generics = prev;
844855
return;
@@ -1688,7 +1699,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16881699
// not used as part of the type system, this is far less surprising.
16891700
this.with_constant_rib(
16901701
IsRepeatExpr::No,
1691-
true,
1702+
HasGenericParams::Yes,
16921703
None,
16931704
|this| this.visit_expr(expr),
16941705
);
@@ -1767,7 +1778,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
17671778
// so it doesn't matter whether this is a trivial constant.
17681779
this.with_constant_rib(
17691780
IsRepeatExpr::No,
1770-
true,
1781+
HasGenericParams::Yes,
17711782
Some((item.ident, constant_item_kind)),
17721783
|this| this.visit_expr(expr),
17731784
);
@@ -1913,20 +1924,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19131924
// Note that we intentionally still forbid `[0; N + 1]` during
19141925
// name resolution so that we don't extend the future
19151926
// compat lint to new cases.
1927+
#[instrument(level = "debug", skip(self, f))]
19161928
fn with_constant_rib(
19171929
&mut self,
19181930
is_repeat: IsRepeatExpr,
1919-
is_trivial: bool,
1931+
may_use_generics: HasGenericParams,
19201932
item: Option<(Ident, ConstantItemKind)>,
19211933
f: impl FnOnce(&mut Self),
19221934
) {
1923-
debug!("with_constant_rib: is_repeat={:?} is_trivial={}", is_repeat, is_trivial);
1924-
self.with_rib(ValueNS, ConstantItemRibKind(is_trivial, item), |this| {
1935+
self.with_rib(ValueNS, ConstantItemRibKind(may_use_generics, item), |this| {
19251936
this.with_rib(
19261937
TypeNS,
1927-
ConstantItemRibKind(is_repeat == IsRepeatExpr::Yes || is_trivial, item),
1938+
ConstantItemRibKind(
1939+
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
1940+
item,
1941+
),
19281942
|this| {
1929-
this.with_label_rib(ConstantItemRibKind(is_trivial, item), f);
1943+
this.with_label_rib(ConstantItemRibKind(may_use_generics, item), f);
19301944
},
19311945
)
19321946
});
@@ -2068,7 +2082,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20682082
// not used as part of the type system, this is far less surprising.
20692083
this.with_constant_rib(
20702084
IsRepeatExpr::No,
2071-
true,
2085+
HasGenericParams::Yes,
20722086
None,
20732087
|this| {
20742088
visit::walk_assoc_item(
@@ -3081,7 +3095,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
30813095
debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat);
30823096
self.with_constant_rib(
30833097
is_repeat,
3084-
constant.value.is_potential_trivial_const_param(),
3098+
if constant.value.is_potential_trivial_const_param() {
3099+
HasGenericParams::Yes
3100+
} else {
3101+
HasGenericParams::No
3102+
},
30853103
None,
30863104
|this| visit::walk_anon_const(this, constant),
30873105
);
@@ -3184,7 +3202,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
31843202
if const_args.contains(&idx) {
31853203
self.with_constant_rib(
31863204
IsRepeatExpr::No,
3187-
argument.is_potential_trivial_const_param(),
3205+
if argument.is_potential_trivial_const_param() {
3206+
HasGenericParams::Yes
3207+
} else {
3208+
HasGenericParams::No
3209+
},
31883210
None,
31893211
|this| {
31903212
this.resolve_expr(argument, None);

0 commit comments

Comments
 (0)