Skip to content

Commit 2bef579

Browse files
committed
Feature-gate associated constants.
1 parent ae56395 commit 2bef579

23 files changed

+95
-1
lines changed

src/doc/reference.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,10 @@ The currently implemented features of the reference compiler are:
24172417
semantics are likely to change, so this macro usage must be opted
24182418
into.
24192419

2420-
* `associated_types` - Allows type aliases in traits. Experimental.
2420+
* `associated_consts` - Allows constants to be defined in `impl` and `trait`
2421+
blocks, so that they can be associated with a type or
2422+
trait in a similar manner to methods and associated
2423+
types.
24212424

24222425
* `box_patterns` - Allows `box` patterns, the exact semantics of which
24232426
is subject to change.

src/libsyntax/feature_gate.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[
153153
// below (it has to be checked before expansion possibly makes
154154
// macros disappear).
155155
("allow_internal_unstable", "1.0.0", Active),
156+
157+
// Allows the definition of associated constants in `trait` or `impl`
158+
// blocks.
159+
("associated_consts", "1.0.0", Active),
156160
];
157161
// (changing above list without updating src/doc/reference.md makes @cmr sad)
158162

@@ -720,6 +724,30 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
720724
}
721725
visit::walk_fn(self, fn_kind, fn_decl, block, span);
722726
}
727+
728+
fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
729+
match ti.node {
730+
ast::ConstTraitItem(..) => {
731+
self.gate_feature("associated_consts",
732+
ti.span,
733+
"associated constants are experimental")
734+
}
735+
_ => {}
736+
}
737+
visit::walk_trait_item(self, ti);
738+
}
739+
740+
fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
741+
match ii.node {
742+
ast::ConstImplItem(..) => {
743+
self.gate_feature("associated_consts",
744+
ii.span,
745+
"associated constants are experimental")
746+
}
747+
_ => {}
748+
}
749+
visit::walk_impl_item(self, ii);
750+
}
723751
}
724752

725753
fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate,

src/test/auxiliary/associated-const-cc-lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
#![crate_type="lib"]
1214

1315
use std::marker::MarkerTrait;

src/test/compile-fail/associated-const-dead-code.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
1112
#![deny(dead_code)]
1213

1314
struct MyFoo;

src/test/compile-fail/associated-const-impl-wrong-type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait Foo: MarkerTrait {

src/test/compile-fail/associated-const-private-impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
mod bar1 {

src/test/compile-fail/associated-const-upper-case-lint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
1112
#![deny(non_upper_case_globals)]
1213
#![allow(dead_code)]
1314

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::marker::MarkerTrait;
12+
13+
trait MyTrait: MarkerTrait {
14+
const C: bool;
15+
//~^ associated constants are experimental
16+
//~| add #![feature(associated_consts)] to the crate attributes to enable
17+
}
18+
19+
struct Foo;
20+
21+
impl Foo {
22+
const C: bool = true;
23+
//~^ associated constants are experimental
24+
//~| add #![feature(associated_consts)] to the crate attributes to enable
25+
}

src/test/compile-fail/impl-wrong-item-for-trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
trait Foo {
1214
fn bar(&self);
1315
const MY_CONST: u32;

src/test/run-pass/associated-const-cross-crate-defaults.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// aux-build:associated-const-cc-lib.rs
1212

13+
#![feature(associated_consts)]
14+
1315
extern crate "associated-const-cc-lib" as foolib;
1416

1517
pub struct LocalFooUseDefault;

src/test/run-pass/associated-const-cross-crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// aux-build:associated-const-cc-lib.rs
1212

13+
#![feature(associated_consts)]
14+
1315
extern crate "associated-const-cc-lib" as foolib;
1416

1517
pub struct LocalFoo;

src/test/run-pass/associated-const-in-global-const.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
struct Foo;
1214

1315
impl Foo {

src/test/run-pass/associated-const-inherent-impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
struct Foo;
1214

1315
impl Foo {

src/test/run-pass/associated-const-marks-live-code.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
#![deny(dead_code)]
1214

1315
const GLOBAL_BAR: u32 = 1;

src/test/run-pass/associated-const-match-patterns.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
struct Foo;

src/test/run-pass/associated-const-overwrite-default.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait Foo: MarkerTrait {

src/test/run-pass/associated-const-public-impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
mod bar1 {

src/test/run-pass/associated-const-resolution-order.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
struct MyType;

src/test/run-pass/associated-const-self-type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait MyInt: MarkerTrait {

src/test/run-pass/associated-const-ufcs-infer-trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait Foo: MarkerTrait {

src/test/run-pass/associated-const-use-default.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait Foo: MarkerTrait {

src/test/run-pass/associated-const-use-impl-of-same-trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
// The main purpose of this test is to ensure that different impls of the same

src/test/run-pass/associated-const.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(associated_consts)]
12+
1113
use std::marker::MarkerTrait;
1214

1315
trait Foo: MarkerTrait {

0 commit comments

Comments
 (0)