Skip to content

Commit c161dcc

Browse files
Rollup merge of #96641 - oli-obk:bool_args, r=wesleywiser
Use a yes/no enum instead of a bool. The bool's meaning wasn't obvious to me at some call sites.
2 parents cffbc43 + 0349f8b commit c161dcc

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;
@@ -1684,7 +1695,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16841695
// not used as part of the type system, this is far less surprising.
16851696
this.with_constant_rib(
16861697
IsRepeatExpr::No,
1687-
true,
1698+
HasGenericParams::Yes,
16881699
None,
16891700
|this| this.visit_expr(expr),
16901701
);
@@ -1763,7 +1774,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
17631774
// so it doesn't matter whether this is a trivial constant.
17641775
this.with_constant_rib(
17651776
IsRepeatExpr::No,
1766-
true,
1777+
HasGenericParams::Yes,
17671778
Some((item.ident, constant_item_kind)),
17681779
|this| this.visit_expr(expr),
17691780
);
@@ -1909,20 +1920,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19091920
// Note that we intentionally still forbid `[0; N + 1]` during
19101921
// name resolution so that we don't extend the future
19111922
// compat lint to new cases.
1923+
#[instrument(level = "debug", skip(self, f))]
19121924
fn with_constant_rib(
19131925
&mut self,
19141926
is_repeat: IsRepeatExpr,
1915-
is_trivial: bool,
1927+
may_use_generics: HasGenericParams,
19161928
item: Option<(Ident, ConstantItemKind)>,
19171929
f: impl FnOnce(&mut Self),
19181930
) {
1919-
debug!("with_constant_rib: is_repeat={:?} is_trivial={}", is_repeat, is_trivial);
1920-
self.with_rib(ValueNS, ConstantItemRibKind(is_trivial, item), |this| {
1931+
self.with_rib(ValueNS, ConstantItemRibKind(may_use_generics, item), |this| {
19211932
this.with_rib(
19221933
TypeNS,
1923-
ConstantItemRibKind(is_repeat == IsRepeatExpr::Yes || is_trivial, item),
1934+
ConstantItemRibKind(
1935+
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
1936+
item,
1937+
),
19241938
|this| {
1925-
this.with_label_rib(ConstantItemRibKind(is_trivial, item), f);
1939+
this.with_label_rib(ConstantItemRibKind(may_use_generics, item), f);
19261940
},
19271941
)
19281942
});
@@ -2064,7 +2078,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20642078
// not used as part of the type system, this is far less surprising.
20652079
this.with_constant_rib(
20662080
IsRepeatExpr::No,
2067-
true,
2081+
HasGenericParams::Yes,
20682082
None,
20692083
|this| {
20702084
visit::walk_assoc_item(
@@ -3077,7 +3091,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
30773091
debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat);
30783092
self.with_constant_rib(
30793093
is_repeat,
3080-
constant.value.is_potential_trivial_const_param(),
3094+
if constant.value.is_potential_trivial_const_param() {
3095+
HasGenericParams::Yes
3096+
} else {
3097+
HasGenericParams::No
3098+
},
30813099
None,
30823100
|this| visit::walk_anon_const(this, constant),
30833101
);
@@ -3180,7 +3198,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
31803198
if const_args.contains(&idx) {
31813199
self.with_constant_rib(
31823200
IsRepeatExpr::No,
3183-
argument.is_potential_trivial_const_param(),
3201+
if argument.is_potential_trivial_const_param() {
3202+
HasGenericParams::Yes
3203+
} else {
3204+
HasGenericParams::No
3205+
},
31843206
None,
31853207
|this| {
31863208
this.resolve_expr(argument, None);

0 commit comments

Comments
 (0)