1
Fork 0

Create specific ConstantHasGenerics for ConstantItemRibKind.

This commit is contained in:
Camille GILLOT 2022-07-14 14:54:53 +02:00
parent a785176741
commit d7d701a9dc
3 changed files with 31 additions and 18 deletions

View file

@ -550,7 +550,7 @@ impl<'a> Resolver<'a> {
} }
} }
if has_generic_params == HasGenericParams::Yes { if let HasGenericParams::Yes = has_generic_params {
// Try to retrieve the span of the function signature and generate a new // Try to retrieve the span of the function signature and generate a new
// message with a local type or const parameter. // message with a local type or const parameter.
let sugg_msg = "try using a local generic parameter instead"; let sugg_msg = "try using a local generic parameter instead";

View file

@ -13,7 +13,9 @@ use rustc_span::{Span, DUMMY_SP};
use std::ptr; use std::ptr;
use crate::late::{ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind}; use crate::late::{
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
};
use crate::macros::{sub_namespace_match, MacroRulesScope}; use crate::macros::{sub_namespace_match, MacroRulesScope};
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize}; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
@ -1180,7 +1182,9 @@ 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 == HasGenericParams::Yes || features.generic_const_exprs) { if !(trivial == ConstantHasGenerics::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.
@ -1253,7 +1257,9 @@ 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 == HasGenericParams::Yes || features.generic_const_exprs) { if !(trivial == ConstantHasGenerics::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

@ -91,13 +91,20 @@ enum PatBoundCtx {
} }
/// Does this the item (from the item rib scope) allow generic parameters? /// Does this the item (from the item rib scope) allow generic parameters?
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug)]
pub(crate) enum HasGenericParams { pub(crate) enum HasGenericParams {
Yes, Yes,
No, No,
} }
impl HasGenericParams { /// May this constant have generics?
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum ConstantHasGenerics {
Yes,
No,
}
impl ConstantHasGenerics {
fn force_yes_if(self, b: bool) -> Self { fn force_yes_if(self, b: bool) -> Self {
if b { Self::Yes } else { self } if b { Self::Yes } else { self }
} }
@ -136,7 +143,7 @@ pub(crate) enum RibKind<'a> {
/// ///
/// The item may reference generic parameters in trivial constant expressions. /// The item may reference generic parameters in trivial constant expressions.
/// All other constants aren't allowed to use generic params at all. /// All other constants aren't allowed to use generic params at all.
ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>), ConstantItemRibKind(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
/// We passed through a module. /// We passed through a module.
ModuleRibKind(Module<'a>), ModuleRibKind(Module<'a>),
@ -995,7 +1002,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
// non-trivial constants this is doesn't matter. // non-trivial constants this is doesn't matter.
self.with_constant_rib( self.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
HasGenericParams::Yes, ConstantHasGenerics::Yes,
None, None,
|this| { |this| {
this.smart_resolve_path( this.smart_resolve_path(
@ -2251,7 +2258,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,
HasGenericParams::Yes, ConstantHasGenerics::Yes,
Some((item.ident, constant_item_kind)), Some((item.ident, constant_item_kind)),
|this| this.visit_expr(expr), |this| this.visit_expr(expr),
); );
@ -2450,7 +2457,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
fn with_constant_rib( fn with_constant_rib(
&mut self, &mut self,
is_repeat: IsRepeatExpr, is_repeat: IsRepeatExpr,
may_use_generics: HasGenericParams, may_use_generics: ConstantHasGenerics,
item: Option<(Ident, ConstantItemKind)>, item: Option<(Ident, ConstantItemKind)>,
f: impl FnOnce(&mut Self), f: impl FnOnce(&mut Self),
) { ) {
@ -2517,7 +2524,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|this| { |this| {
this.with_constant_rib( this.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
HasGenericParams::Yes, ConstantHasGenerics::Yes,
None, None,
|this| this.visit_expr(expr), |this| this.visit_expr(expr),
) )
@ -2689,7 +2696,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| { self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
this.with_constant_rib( this.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
HasGenericParams::Yes, ConstantHasGenerics::Yes,
None, None,
|this| this.visit_expr(expr), |this| this.visit_expr(expr),
) )
@ -3696,9 +3703,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.with_constant_rib( self.with_constant_rib(
is_repeat, is_repeat,
if constant.value.is_potential_trivial_const_param() { if constant.value.is_potential_trivial_const_param() {
HasGenericParams::Yes ConstantHasGenerics::Yes
} else { } else {
HasGenericParams::No ConstantHasGenerics::No
}, },
None, None,
|this| visit::walk_anon_const(this, constant), |this| visit::walk_anon_const(this, constant),
@ -3707,8 +3714,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
fn resolve_inline_const(&mut self, constant: &'ast AnonConst) { fn resolve_inline_const(&mut self, constant: &'ast AnonConst) {
debug!("resolve_anon_const {constant:?}"); debug!("resolve_anon_const {constant:?}");
self.with_constant_rib(IsRepeatExpr::No, HasGenericParams::Yes, None, |this| { self.with_constant_rib(IsRepeatExpr::No, ConstantHasGenerics::Yes, None, |this| {
visit::walk_anon_const(this, constant); visit::walk_anon_const(this, constant)
}); });
} }
@ -3814,9 +3821,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.with_constant_rib( self.with_constant_rib(
IsRepeatExpr::No, IsRepeatExpr::No,
if argument.is_potential_trivial_const_param() { if argument.is_potential_trivial_const_param() {
HasGenericParams::Yes ConstantHasGenerics::Yes
} else { } else {
HasGenericParams::No ConstantHasGenerics::No
}, },
None, None,
|this| { |this| {