Skip to content

Commit 49cbfa1

Browse files
committed
pre-expansion gate const_generics
1 parent 04c661b commit 49cbfa1

9 files changed

+37
-26
lines changed

src/libsyntax/feature_gate/check.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::accepted::ACCEPTED_FEATURES;
33
use super::removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};
44
use super::builtin_attrs::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
55

6-
use crate::ast::{self, NodeId, GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData};
6+
use crate::ast::{self, NodeId, PatKind, RangeEnd, VariantData};
77
use crate::attr::{self, check_builtin_attribute};
88
use crate::source_map::Spanned;
99
use crate::edition::{ALL_EDITIONS, Edition};
@@ -571,16 +571,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
571571
visit::walk_fn(self, fn_kind, fn_decl, span)
572572
}
573573

574-
fn visit_generic_param(&mut self, param: &'a GenericParam) {
575-
match param.kind {
576-
GenericParamKind::Const { .. } =>
577-
gate_feature_post!(&self, const_generics, param.ident.span,
578-
"const generics are unstable"),
579-
_ => {}
580-
}
581-
visit::walk_generic_param(self, param)
582-
}
583-
584574
fn visit_trait_item(&mut self, ti: &'a ast::TraitItem) {
585575
match ti.kind {
586576
ast::TraitItemKind::Method(ref sig, ref block) => {
@@ -840,6 +830,7 @@ pub fn check_crate(krate: &ast::Crate,
840830
gate_all!(trait_alias, "trait aliases are experimental");
841831
gate_all!(associated_type_bounds, "associated type bounds are unstable");
842832
gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
833+
gate_all!(const_generics, "const generics are unstable");
843834

844835
visit::walk_crate(&mut visitor, krate);
845836
}

src/libsyntax/parse/parser/generics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ impl<'a> Parser<'a> {
5555
}
5656

5757
fn parse_const_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, GenericParam> {
58+
let lo = self.token.span;
59+
5860
self.expect_keyword(kw::Const)?;
5961
let ident = self.parse_ident()?;
6062
self.expect(&token::Colon)?;
6163
let ty = self.parse_ty()?;
6264

65+
self.sess.gated_spans.const_generics.borrow_mut().push(lo.to(self.prev_span));
66+
6367
Ok(GenericParam {
6468
ident,
6569
id: ast::DUMMY_NODE_ID,

src/libsyntax/sess.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ crate struct GatedSpans {
3636
pub associated_type_bounds: Lock<Vec<Span>>,
3737
/// Spans collected for gating `crate_visibility_modifier`, e.g. `crate fn`.
3838
pub crate_visibility_modifier: Lock<Vec<Span>>,
39+
/// Spans collected for gating `const_generics`, e.g. `const N: usize`.
40+
pub const_generics: Lock<Vec<Span>>,
3941
}
4042

4143
/// Info about a parsing session.

src/test/ui/const-generics/const-param-in-trait-ungated.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: const generics are unstable
2-
--> $DIR/const-param-in-trait-ungated.rs:1:19
2+
--> $DIR/const-param-in-trait-ungated.rs:1:13
33
|
44
LL | trait Trait<const T: ()> {}
5-
| ^
5+
| ^^^^^^^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
88
= help: add `#![feature(const_generics)]` to the crate attributes to enable

src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | struct B<T, const N: T>(PhantomData<[T; N]>);
55
| ^ const parameter depends on type parameter
66

77
error[E0658]: const generics are unstable
8-
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:19
8+
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:13
99
|
1010
LL | struct B<T, const N: T>(PhantomData<[T; N]>);
11-
| ^
11+
| ^^^^^^^^^^
1212
|
1313
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
1414
= help: add `#![feature(const_generics)]` to the crate attributes to enable

src/test/ui/const-generics/issues/issue-60263.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: const generics are unstable
2-
--> $DIR/issue-60263.rs:1:16
2+
--> $DIR/issue-60263.rs:1:10
33
|
44
LL | struct B<const I: u8>;
5-
| ^
5+
| ^^^^^^^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
88
= help: add `#![feature(const_generics)]` to the crate attributes to enable

src/test/ui/feature-gates/feature-gate-const_generics-ptr.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0658]: const generics are unstable
2-
--> $DIR/feature-gate-const_generics-ptr.rs:1:22
2+
--> $DIR/feature-gate-const_generics-ptr.rs:1:16
33
|
44
LL | struct ConstFn<const F: fn()>;
5-
| ^
5+
| ^^^^^^^^^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
88
= help: add `#![feature(const_generics)]` to the crate attributes to enable
99

1010
error[E0658]: const generics are unstable
11-
--> $DIR/feature-gate-const_generics-ptr.rs:5:23
11+
--> $DIR/feature-gate-const_generics-ptr.rs:5:17
1212
|
1313
LL | struct ConstPtr<const P: *const u32>;
14-
| ^
14+
| ^^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
1717
= help: add `#![feature(const_generics)]` to the crate attributes to enable

src/test/ui/feature-gates/feature-gate-const_generics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ fn foo<const X: ()>() {} //~ ERROR const generics are unstable
22

33
struct Foo<const X: usize>([(); X]); //~ ERROR const generics are unstable
44

5+
macro_rules! accept_item { ($i:item) => {} }
6+
accept_item! {
7+
impl<const X: ()> A {} //~ ERROR const generics are unstable
8+
}
9+
510
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
error[E0658]: const generics are unstable
2-
--> $DIR/feature-gate-const_generics.rs:1:14
2+
--> $DIR/feature-gate-const_generics.rs:1:8
33
|
44
LL | fn foo<const X: ()>() {}
5-
| ^
5+
| ^^^^^^^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
88
= help: add `#![feature(const_generics)]` to the crate attributes to enable
99

1010
error[E0658]: const generics are unstable
11-
--> $DIR/feature-gate-const_generics.rs:3:18
11+
--> $DIR/feature-gate-const_generics.rs:3:12
1212
|
1313
LL | struct Foo<const X: usize>([(); X]);
14-
| ^
14+
| ^^^^^^^^^^^^^^
1515
|
1616
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
1717
= help: add `#![feature(const_generics)]` to the crate attributes to enable
1818

19-
error: aborting due to 2 previous errors
19+
error[E0658]: const generics are unstable
20+
--> $DIR/feature-gate-const_generics.rs:7:10
21+
|
22+
LL | impl<const X: ()> A {}
23+
| ^^^^^^^^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/44580
26+
= help: add `#![feature(const_generics)]` to the crate attributes to enable
27+
28+
error: aborting due to 3 previous errors
2029

2130
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)