Use a yes/no enum instead of a bool.

The bool's meaning wasn't obvious to me at some call sites.
This commit is contained in:
Oli Scherer 2022-05-02 15:36:48 +00:00
parent 5428983286
commit 0349f8bd79
2 changed files with 49 additions and 27 deletions

View File

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

View File

@ -94,6 +94,12 @@ crate enum HasGenericParams {
No, No,
} }
impl HasGenericParams {
fn force_yes_if(self, b: bool) -> Self {
if b { Self::Yes } else { self }
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
crate enum ConstantItemKind { crate enum ConstantItemKind {
Const, Const,
@ -125,9 +131,9 @@ crate enum RibKind<'a> {
/// We're in a constant item. Can't refer to dynamic stuff. /// We're in a constant item. Can't refer to dynamic stuff.
/// ///
/// The `bool` indicates if this constant may reference generic parameters /// The item may reference generic parameters in trivial constant expressions.
/// and is used to only allow generic parameters to be used in trivial constant expressions. /// All other constants aren't allowed to use generic params at all.
ConstantItemRibKind(bool, Option<(Ident, ConstantItemKind)>), ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>),
/// We passed through a module. /// We passed through a module.
ModuleRibKind(Module<'a>), ModuleRibKind(Module<'a>),
@ -826,19 +832,24 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
// Note that we might not be inside of an repeat expression here, // Note that we might not be inside of an repeat expression here,
// but considering that `IsRepeatExpr` is only relevant for // but considering that `IsRepeatExpr` is only relevant for
// non-trivial constants this is doesn't matter. // non-trivial constants this is doesn't matter.
self.with_constant_rib(IsRepeatExpr::No, true, None, |this| { self.with_constant_rib(
this.smart_resolve_path( IsRepeatExpr::No,
ty.id, HasGenericParams::Yes,
qself.as_ref(), None,
path, |this| {
PathSource::Expr(None), this.smart_resolve_path(
); ty.id,
qself.as_ref(),
path,
PathSource::Expr(None),
);
if let Some(ref qself) = *qself { if let Some(ref qself) = *qself {
this.visit_ty(&qself.ty); this.visit_ty(&qself.ty);
} }
this.visit_path(path, ty.id); this.visit_path(path, ty.id);
}); },
);
self.diagnostic_metadata.currently_processing_generics = prev; self.diagnostic_metadata.currently_processing_generics = prev;
return; return;
@ -1688,7 +1699,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// not used as part of the type system, this is far less surprising. // not used as part of the type system, this is far less surprising.
this.with_constant_rib( this.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
true, HasGenericParams::Yes,
None, None,
|this| this.visit_expr(expr), |this| this.visit_expr(expr),
); );
@ -1767,7 +1778,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// so it doesn't matter whether this is a trivial constant. // so it doesn't matter whether this is a trivial constant.
this.with_constant_rib( this.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
true, HasGenericParams::Yes,
Some((item.ident, constant_item_kind)), Some((item.ident, constant_item_kind)),
|this| this.visit_expr(expr), |this| this.visit_expr(expr),
); );
@ -1913,20 +1924,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// Note that we intentionally still forbid `[0; N + 1]` during // Note that we intentionally still forbid `[0; N + 1]` during
// name resolution so that we don't extend the future // name resolution so that we don't extend the future
// compat lint to new cases. // compat lint to new cases.
#[instrument(level = "debug", skip(self, f))]
fn with_constant_rib( fn with_constant_rib(
&mut self, &mut self,
is_repeat: IsRepeatExpr, is_repeat: IsRepeatExpr,
is_trivial: bool, may_use_generics: HasGenericParams,
item: Option<(Ident, ConstantItemKind)>, item: Option<(Ident, ConstantItemKind)>,
f: impl FnOnce(&mut Self), f: impl FnOnce(&mut Self),
) { ) {
debug!("with_constant_rib: is_repeat={:?} is_trivial={}", is_repeat, is_trivial); self.with_rib(ValueNS, ConstantItemRibKind(may_use_generics, item), |this| {
self.with_rib(ValueNS, ConstantItemRibKind(is_trivial, item), |this| {
this.with_rib( this.with_rib(
TypeNS, TypeNS,
ConstantItemRibKind(is_repeat == IsRepeatExpr::Yes || is_trivial, item), ConstantItemRibKind(
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
item,
),
|this| { |this| {
this.with_label_rib(ConstantItemRibKind(is_trivial, item), f); this.with_label_rib(ConstantItemRibKind(may_use_generics, item), f);
}, },
) )
}); });
@ -2068,7 +2082,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// not used as part of the type system, this is far less surprising. // not used as part of the type system, this is far less surprising.
this.with_constant_rib( this.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
true, HasGenericParams::Yes,
None, None,
|this| { |this| {
visit::walk_assoc_item( visit::walk_assoc_item(
@ -3081,7 +3095,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat); debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat);
self.with_constant_rib( self.with_constant_rib(
is_repeat, is_repeat,
constant.value.is_potential_trivial_const_param(), if constant.value.is_potential_trivial_const_param() {
HasGenericParams::Yes
} else {
HasGenericParams::No
},
None, None,
|this| visit::walk_anon_const(this, constant), |this| visit::walk_anon_const(this, constant),
); );
@ -3184,7 +3202,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if const_args.contains(&idx) { if const_args.contains(&idx) {
self.with_constant_rib( self.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
argument.is_potential_trivial_const_param(), if argument.is_potential_trivial_const_param() {
HasGenericParams::Yes
} else {
HasGenericParams::No
},
None, None,
|this| { |this| {
this.resolve_expr(argument, None); this.resolve_expr(argument, None);