Skip to content

Commit 00267eb

Browse files
committed
Feature-gate associated constants.
1 parent 93d70a8 commit 00267eb

23 files changed

+95
-1
lines changed

src/doc/reference.md

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

2353-
* `associated_types` - Allows type aliases in traits. Experimental.
2353+
* `associated_consts` - Allows constants to be defined in `impl` and `trait`
2354+
blocks, so that they can be associated with a type or
2355+
trait in a similar manner to methods and associated
2356+
types.
23542357

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

src/libsyntax/feature_gate.rs

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

154154
// #23121. Array patterns have some hazards yet.
155155
("slice_patterns", "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

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

661689
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)