Skip to content

Commit d7d701a

Browse files
committed
Create specific ConstantHasGenerics for ConstantItemRibKind.
1 parent a785176 commit d7d701a

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'a> Resolver<'a> {
550550
}
551551
}
552552

553-
if has_generic_params == HasGenericParams::Yes {
553+
if let HasGenericParams::Yes = has_generic_params {
554554
// Try to retrieve the span of the function signature and generate a new
555555
// message with a local type or const parameter.
556556
let sugg_msg = "try using a local generic parameter instead";

compiler/rustc_resolve/src/ident.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use rustc_span::{Span, DUMMY_SP};
1313

1414
use std::ptr;
1515

16-
use crate::late::{ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind};
16+
use crate::late::{
17+
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
18+
};
1719
use crate::macros::{sub_namespace_match, MacroRulesScope};
1820
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
1921
use crate::{ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
@@ -1180,7 +1182,9 @@ impl<'a> Resolver<'a> {
11801182
ConstantItemRibKind(trivial, _) => {
11811183
let features = self.session.features_untracked();
11821184
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1183-
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1185+
if !(trivial == ConstantHasGenerics::Yes
1186+
|| features.generic_const_exprs)
1187+
{
11841188
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
11851189
// we can't easily tell if it's generic at this stage, so we instead remember
11861190
// this and then enforce the self type to be concrete later on.
@@ -1253,7 +1257,9 @@ impl<'a> Resolver<'a> {
12531257
ConstantItemRibKind(trivial, _) => {
12541258
let features = self.session.features_untracked();
12551259
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
1256-
if !(trivial == HasGenericParams::Yes || features.generic_const_exprs) {
1260+
if !(trivial == ConstantHasGenerics::Yes
1261+
|| features.generic_const_exprs)
1262+
{
12571263
if let Some(span) = finalize {
12581264
self.report_error(
12591265
span,

compiler/rustc_resolve/src/late.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ enum PatBoundCtx {
9191
}
9292

9393
/// Does this the item (from the item rib scope) allow generic parameters?
94-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
94+
#[derive(Copy, Clone, Debug)]
9595
pub(crate) enum HasGenericParams {
9696
Yes,
9797
No,
9898
}
9999

100-
impl HasGenericParams {
100+
/// May this constant have generics?
101+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
102+
pub(crate) enum ConstantHasGenerics {
103+
Yes,
104+
No,
105+
}
106+
107+
impl ConstantHasGenerics {
101108
fn force_yes_if(self, b: bool) -> Self {
102109
if b { Self::Yes } else { self }
103110
}
@@ -136,7 +143,7 @@ pub(crate) enum RibKind<'a> {
136143
///
137144
/// The item may reference generic parameters in trivial constant expressions.
138145
/// All other constants aren't allowed to use generic params at all.
139-
ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>),
146+
ConstantItemRibKind(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
140147

141148
/// We passed through a module.
142149
ModuleRibKind(Module<'a>),
@@ -995,7 +1002,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
9951002
// non-trivial constants this is doesn't matter.
9961003
self.with_constant_rib(
9971004
IsRepeatExpr::No,
998-
HasGenericParams::Yes,
1005+
ConstantHasGenerics::Yes,
9991006
None,
10001007
|this| {
10011008
this.smart_resolve_path(
@@ -2251,7 +2258,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22512258
// so it doesn't matter whether this is a trivial constant.
22522259
this.with_constant_rib(
22532260
IsRepeatExpr::No,
2254-
HasGenericParams::Yes,
2261+
ConstantHasGenerics::Yes,
22552262
Some((item.ident, constant_item_kind)),
22562263
|this| this.visit_expr(expr),
22572264
);
@@ -2450,7 +2457,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
24502457
fn with_constant_rib(
24512458
&mut self,
24522459
is_repeat: IsRepeatExpr,
2453-
may_use_generics: HasGenericParams,
2460+
may_use_generics: ConstantHasGenerics,
24542461
item: Option<(Ident, ConstantItemKind)>,
24552462
f: impl FnOnce(&mut Self),
24562463
) {
@@ -2517,7 +2524,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
25172524
|this| {
25182525
this.with_constant_rib(
25192526
IsRepeatExpr::No,
2520-
HasGenericParams::Yes,
2527+
ConstantHasGenerics::Yes,
25212528
None,
25222529
|this| this.visit_expr(expr),
25232530
)
@@ -2689,7 +2696,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
26892696
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
26902697
this.with_constant_rib(
26912698
IsRepeatExpr::No,
2692-
HasGenericParams::Yes,
2699+
ConstantHasGenerics::Yes,
26932700
None,
26942701
|this| this.visit_expr(expr),
26952702
)
@@ -3696,9 +3703,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
36963703
self.with_constant_rib(
36973704
is_repeat,
36983705
if constant.value.is_potential_trivial_const_param() {
3699-
HasGenericParams::Yes
3706+
ConstantHasGenerics::Yes
37003707
} else {
3701-
HasGenericParams::No
3708+
ConstantHasGenerics::No
37023709
},
37033710
None,
37043711
|this| visit::walk_anon_const(this, constant),
@@ -3707,8 +3714,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
37073714

37083715
fn resolve_inline_const(&mut self, constant: &'ast AnonConst) {
37093716
debug!("resolve_anon_const {constant:?}");
3710-
self.with_constant_rib(IsRepeatExpr::No, HasGenericParams::Yes, None, |this| {
3711-
visit::walk_anon_const(this, constant);
3717+
self.with_constant_rib(IsRepeatExpr::No, ConstantHasGenerics::Yes, None, |this| {
3718+
visit::walk_anon_const(this, constant)
37123719
});
37133720
}
37143721

@@ -3814,9 +3821,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
38143821
self.with_constant_rib(
38153822
IsRepeatExpr::No,
38163823
if argument.is_potential_trivial_const_param() {
3817-
HasGenericParams::Yes
3824+
ConstantHasGenerics::Yes
38183825
} else {
3819-
HasGenericParams::No
3826+
ConstantHasGenerics::No
38203827
},
38213828
None,
38223829
|this| {

0 commit comments

Comments
 (0)