Skip to content

Commit 3e531ed

Browse files
committed
Gate default type parameter overrides.
Fixes #12423.
1 parent 6720977 commit 3e531ed

File tree

12 files changed

+47
-47
lines changed

12 files changed

+47
-47
lines changed

src/librustc/driver/driver.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ pub fn phase_3_run_analysis_passes(sess: Session,
296296
let region_map = time(time_passes, "region resolution", (), |_|
297297
middle::region::resolve_crate(sess, krate));
298298

299-
let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map, freevars,
300-
region_map, lang_items);
299+
let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map,
300+
freevars, region_map, lang_items);
301301

302302
// passes are timed inside typeck
303303
let (method_map, vtable_map) = typeck::check_crate(ty_cx, trait_map, krate);
@@ -976,6 +976,7 @@ pub fn build_session_(sopts: @session::Options,
976976
lints: RefCell::new(HashMap::new()),
977977
node_id: Cell::new(1),
978978
crate_types: @RefCell::new(~[]),
979+
features: front::feature_gate::Features::new()
979980
}
980981
}
981982

src/librustc/driver/session.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use back::target_strs;
1313
use back;
1414
use driver::driver::host_triple;
15+
use front;
1516
use metadata::filesearch;
1617
use metadata;
1718
use middle::lint;
@@ -186,6 +187,7 @@ pub struct Session_ {
186187
~[(lint::Lint, codemap::Span, ~str)]>>,
187188
node_id: Cell<ast::NodeId>,
188189
crate_types: @RefCell<~[CrateType]>,
190+
features: front::feature_gate::Features
189191
}
190192

191193
pub type Session = @Session_;

src/librustc/front/feature_gate.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use syntax::parse::token;
3030

3131
use driver::session::Session;
3232

33+
use std::cell::Cell;
34+
3335
/// This is a list of all known features since the beginning of time. This list
3436
/// can never shrink, it may only be expanded (in order to prevent old programs
3537
/// from failing to compile). The status of each feature may change, however.
@@ -69,6 +71,19 @@ enum Status {
6971
Accepted,
7072
}
7173

74+
/// A set of features to be used by later passes.
75+
pub struct Features {
76+
default_type_params: Cell<bool>
77+
}
78+
79+
impl Features {
80+
pub fn new() -> Features {
81+
Features {
82+
default_type_params: Cell::new(false)
83+
}
84+
}
85+
}
86+
7287
struct Context {
7388
features: ~[&'static str],
7489
sess: Session,
@@ -315,4 +330,6 @@ pub fn check_crate(sess: Session, krate: &ast::Crate) {
315330
visit::walk_crate(&mut cx, krate, ());
316331

317332
sess.abort_if_errors();
333+
334+
sess.features.default_type_params.set(cx.has_feature("default_type_params"));
318335
}

src/librustc/middle/lint.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ pub enum Lint {
8888
AttributeUsage,
8989
UnknownFeatures,
9090
UnknownCrateType,
91-
DefaultTypeParamUsage,
9291

9392
ManagedHeapMemory,
9493
OwnedHeapMemory,
@@ -382,14 +381,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
382381
lint: UnusedResult,
383382
desc: "unused result of an expression in a statement",
384383
default: allow,
385-
}),
386-
387-
("default_type_param_usage",
388-
LintSpec {
389-
lint: DefaultTypeParamUsage,
390-
desc: "prevents explicitly setting a type parameter with a default",
391-
default: deny,
392-
}),
384+
})
393385
];
394386

395387
/*

src/librustc/middle/ty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ pub fn mk_ctxt(s: session::Session,
10791079
region_maps: middle::region::RegionMaps,
10801080
lang_items: @middle::lang_items::LanguageItems)
10811081
-> ctxt {
1082+
10821083
@ctxt_ {
10831084
named_region_map: named_region_map,
10841085
item_variance_map: RefCell::new(HashMap::new()),
@@ -1126,7 +1127,7 @@ pub fn mk_ctxt(s: session::Session,
11261127
upvar_borrow_map: RefCell::new(HashMap::new()),
11271128
extern_const_statics: RefCell::new(HashMap::new()),
11281129
extern_const_variants: RefCell::new(HashMap::new()),
1129-
}
1130+
}
11301131
}
11311132

11321133
// Type constructors

src/librustc/middle/typeck/astconv.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252

5353
use middle::const_eval;
54-
use middle::lint;
5554
use middle::subst::Subst;
5655
use middle::ty::{substs};
5756
use middle::ty::{ty_param_substs_and_ty};
@@ -219,11 +218,12 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
219218
expected, formal_ty_param_count, supplied_ty_param_count));
220219
}
221220

222-
if supplied_ty_param_count > required_ty_param_count {
223-
let id = path.segments.iter().flat_map(|s| s.types.iter())
224-
.nth(required_ty_param_count).unwrap().id;
225-
this.tcx().sess.add_lint(lint::DefaultTypeParamUsage, id, path.span,
226-
~"provided type arguments with defaults");
221+
if supplied_ty_param_count > required_ty_param_count
222+
&& !this.tcx().sess.features.default_type_params.get() {
223+
this.tcx().sess.span_err(path.span, "default type parameters are \
224+
experimental and possibly buggy");
225+
this.tcx().sess.span_note(path.span, "add #[feature(default_type_params)] \
226+
to the crate attributes to enable");
227227
}
228228

229229
let tps = path.segments.iter().flat_map(|s| s.types.iter())

src/librustc/middle/typeck/check/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ use middle::const_eval;
8181
use middle::lang_items::{ExchangeHeapLangItem, GcLangItem};
8282
use middle::lang_items::{ManagedHeapLangItem};
8383
use middle::lint::UnreachableCode;
84-
use middle::lint;
8584
use middle::pat_util::pat_id_map;
8685
use middle::pat_util;
8786
use middle::subst::Subst;
@@ -3750,9 +3749,12 @@ pub fn instantiate_path(fcx: @FnCtxt,
37503749
expected, user_ty_param_req, ty_substs_len));
37513750
(fcx.infcx().next_ty_vars(ty_param_count), regions)
37523751
} else {
3753-
if ty_substs_len > user_ty_param_req {
3754-
fcx.tcx().sess.add_lint(lint::DefaultTypeParamUsage, node_id, pth.span,
3755-
~"provided type arguments with defaults");
3752+
if ty_substs_len > user_ty_param_req
3753+
&& !fcx.tcx().sess.features.default_type_params.get() {
3754+
fcx.tcx().sess.span_err(pth.span, "default type parameters are \
3755+
experimental and possibly buggy");
3756+
fcx.tcx().sess.span_note(pth.span, "add #[feature(default_type_params)] \
3757+
to the crate attributes to enable");
37563758
}
37573759

37583760
// Build up the list of type parameters, inserting the self parameter

src/libstd/lib.rs

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

5757
// Turn on default type parameters.
5858
#[feature(default_type_params)];
59+
// NOTE remove the following two attributes after the next snapshot.
60+
#[allow(unrecognized_lint)];
5961
#[allow(default_type_param_usage)];
6062

6163
// Don't link to std. We are std.

src/libsyntax/ext/deriving/hash.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,25 @@ use codemap::Span;
1313
use ext::base::ExtCtxt;
1414
use ext::build::AstBuilder;
1515
use ext::deriving::generic::*;
16-
use parse::token::InternedString;
1716

1817
pub fn expand_deriving_hash(cx: &mut ExtCtxt,
1918
span: Span,
2019
mitem: @MetaItem,
2120
item: @Item,
2221
push: |@Item|) {
2322

24-
let allow_default_type_param_usage = cx.attribute(
25-
span,
26-
cx.meta_list(
27-
span,
28-
InternedString::new("allow"),
29-
~[cx.meta_word(span, InternedString::new("default_type_param_usage"))]));
30-
3123
let hash_trait_def = TraitDef {
3224
span: span,
33-
attributes: ~[allow_default_type_param_usage],
34-
path: Path::new_(~["std", "hash", "Hash"], None,
35-
~[~Literal(Path::new_local("__H"))], true),
25+
attributes: ~[],
26+
path: Path::new(~["std", "hash", "Hash"]),
3627
additional_bounds: ~[],
37-
generics: LifetimeBounds {
38-
lifetimes: ~[],
39-
bounds: ~[("__H", ~[Path::new(~["std", "io", "Writer"])])],
40-
},
28+
generics: LifetimeBounds::empty(),
4129
methods: ~[
4230
MethodDef {
4331
name: "hash",
4432
generics: LifetimeBounds::empty(),
4533
explicit_self: borrowed_explicit_self(),
46-
args: ~[Ptr(~Literal(Path::new_local("__H")),
34+
args: ~[Ptr(~Literal(Path::new(~["std", "hash", "sip", "SipState"])),
4735
Borrowed(None, MutMutable))],
4836
ret_ty: nil_ty(),
4937
inline: true,

src/test/compile-fail/lint-default-type-param-usage.rs renamed to src/test/compile-fail/gated-default-type-param-usage.rs

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

11-
#[feature(default_type_params)];
11+
// aux-build:default_type_params_xc.rs
1212

1313
#[deny(default_type_param_usage)];
1414

15-
pub struct Heap;
16-
17-
pub struct Vec<T, A = Heap>;
15+
extern crate default_type_params_xc;
1816

1917
pub struct FooAlloc;
2018

21-
pub type VecFoo<T> = Vec<T, FooAlloc>; //~ ERROR provided type arguments with defaults
19+
pub type VecFoo<T> = default_type_params_xc::FakeVec<T, FooAlloc>;
20+
//~^ ERROR: default type parameters are experimental
2221

2322
fn main() {}

src/test/run-pass/generic-default-type-params-cross-crate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
// ignore-fast #[feature] doesn't work with check-fast
1414
#[feature(default_type_params)];
1515

16-
#[allow(default_type_param_usage)];
17-
1816
extern crate default_type_params_xc;
1917

2018
struct Vec<T, A = default_type_params_xc::Heap>;

src/test/run-pass/generic-default-type-params.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// ignore-fast #[feature] doesn't work with check-fast
1212
#[feature(default_type_params)];
1313

14-
#[allow(default_type_param_usage)];
15-
1614
struct Foo<A = (int, char)> {
1715
a: A
1816
}

0 commit comments

Comments
 (0)