Skip to content

Commit 2cd6cd6

Browse files
committed
Feature-gate associated constants.
1 parent 43bc897 commit 2cd6cd6

23 files changed

+95
-1
lines changed

src/doc/reference.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,10 @@ The currently implemented features of the reference compiler are:
23552355
semantics are likely to change, so this macro usage must be opted
23562356
into.
23572357

2358-
* `associated_types` - Allows type aliases in traits. Experimental.
2358+
* `associated_consts` - Allows constants to be defined in `impl` and `trait`
2359+
blocks, so that they can be associated with a type or
2360+
trait in a similar manner to methods and associated
2361+
types.
23592362

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

src/libsyntax/feature_gate.rs

+28
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[
155155

156156
// Allows use of unary negate on unsigned integers, e.g. -e for e: u8
157157
("negate_unsigned", "1.0.0", Active),
158+
159+
// Allows the definition of associated constants in `trait` or `impl`
160+
// blocks.
161+
("associated_consts", "1.0.0", Active),
158162
];
159163
// (changing above list without updating src/doc/reference.md makes @cmr sad)
160164

@@ -659,6 +663,30 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
659663
}
660664
visit::walk_fn(self, fn_kind, fn_decl, block, span);
661665
}
666+
667+
fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
668+
match ti.node {
669+
ast::ConstTraitItem(..) => {
670+
self.gate_feature("associated_consts",
671+
ti.span,
672+
"associated constants are experimental")
673+
}
674+
_ => {}
675+
}
676+
visit::walk_trait_item(self, ti);
677+
}
678+
679+
fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
680+
match ii.node {
681+
ast::ConstImplItem(..) => {
682+
self.gate_feature("associated_consts",
683+
ii.span,
684+
"associated constants are experimental")
685+
}
686+
_ => {}
687+
}
688+
visit::walk_impl_item(self, ii);
689+
}
662690
}
663691

664692
fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,

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

+2
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

+1
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

+2
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

+2
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

+1
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

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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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)