]> git.lizzy.rs Git - rust.git/commitdiff
Gate default type parameter overrides.
authorEduard Burtescu <edy.burt@gmail.com>
Mon, 24 Feb 2014 20:45:31 +0000 (22:45 +0200)
committerEduard Burtescu <edy.burt@gmail.com>
Mon, 24 Feb 2014 20:45:31 +0000 (22:45 +0200)
Fixes #12423.

13 files changed:
src/librustc/driver/driver.rs
src/librustc/driver/session.rs
src/librustc/front/feature_gate.rs
src/librustc/middle/lint.rs
src/librustc/middle/ty.rs
src/librustc/middle/typeck/astconv.rs
src/librustc/middle/typeck/check/mod.rs
src/libstd/lib.rs
src/libsyntax/ext/deriving/hash.rs
src/test/compile-fail/gated-default-type-param-usage.rs [new file with mode: 0644]
src/test/compile-fail/lint-default-type-param-usage.rs [deleted file]
src/test/run-pass/generic-default-type-params-cross-crate.rs
src/test/run-pass/generic-default-type-params.rs

index 1f7a75da345168034a444501c57358a8ab2f37d0..c82e70e7c0ddf8a2ac50c136f74901cda71639d0 100644 (file)
@@ -296,8 +296,8 @@ pub fn phase_3_run_analysis_passes(sess: Session,
     let region_map = time(time_passes, "region resolution", (), |_|
                           middle::region::resolve_crate(sess, krate));
 
-    let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map, freevars,
-                            region_map, lang_items);
+    let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map,
+                            freevars, region_map, lang_items);
 
     // passes are timed inside typeck
     let (method_map, vtable_map) = typeck::check_crate(ty_cx, trait_map, krate);
@@ -976,6 +976,7 @@ pub fn build_session_(sopts: @session::Options,
         lints: RefCell::new(HashMap::new()),
         node_id: Cell::new(1),
         crate_types: @RefCell::new(~[]),
+        features: front::feature_gate::Features::new()
     }
 }
 
index e023190e5f4e709d80e1d3809b095fb5b183c453..9a33c54d50f2bdd908fa7f78ea35359226174982 100644 (file)
@@ -12,6 +12,7 @@
 use back::target_strs;
 use back;
 use driver::driver::host_triple;
+use front;
 use metadata::filesearch;
 use metadata;
 use middle::lint;
@@ -186,6 +187,7 @@ pub struct Session_ {
                            ~[(lint::Lint, codemap::Span, ~str)]>>,
     node_id: Cell<ast::NodeId>,
     crate_types: @RefCell<~[CrateType]>,
+    features: front::feature_gate::Features
 }
 
 pub type Session = @Session_;
index 52553af53c76d17c7a4bfb34b884cee8dd70d4e4..813bceafed546d51a34b4a59a0509482560a1be2 100644 (file)
@@ -30,6 +30,8 @@
 
 use driver::session::Session;
 
+use std::cell::Cell;
+
 /// This is a list of all known features since the beginning of time. This list
 /// can never shrink, it may only be expanded (in order to prevent old programs
 /// from failing to compile). The status of each feature may change, however.
@@ -69,6 +71,19 @@ enum Status {
     Accepted,
 }
 
+/// A set of features to be used by later passes.
+pub struct Features {
+    default_type_params: Cell<bool>
+}
+
+impl Features {
+    pub fn new() -> Features {
+        Features {
+            default_type_params: Cell::new(false)
+        }
+    }
+}
+
 struct Context {
     features: ~[&'static str],
     sess: Session,
@@ -315,4 +330,6 @@ pub fn check_crate(sess: Session, krate: &ast::Crate) {
     visit::walk_crate(&mut cx, krate, ());
 
     sess.abort_if_errors();
+
+    sess.features.default_type_params.set(cx.has_feature("default_type_params"));
 }
index 36ed4913cdb0cd7bd2e732084787cf6794702c32..242b9906c72f9117c652fcc494786a23e348482a 100644 (file)
@@ -88,7 +88,6 @@ pub enum Lint {
     AttributeUsage,
     UnknownFeatures,
     UnknownCrateType,
-    DefaultTypeParamUsage,
 
     ManagedHeapMemory,
     OwnedHeapMemory,
@@ -382,14 +381,7 @@ enum LintSource {
         lint: UnusedResult,
         desc: "unused result of an expression in a statement",
         default: allow,
-    }),
-
-     ("default_type_param_usage",
-     LintSpec {
-         lint: DefaultTypeParamUsage,
-         desc: "prevents explicitly setting a type parameter with a default",
-         default: deny,
-     }),
+    })
 ];
 
 /*
index b351b5c0cb8a1b97b4087e1f69a89e5a7aa3ffbb..7a6de3ef5ddce65daedb665471b2d78403901f0f 100644 (file)
@@ -1079,6 +1079,7 @@ pub fn mk_ctxt(s: session::Session,
                region_maps: middle::region::RegionMaps,
                lang_items: @middle::lang_items::LanguageItems)
             -> ctxt {
+
     @ctxt_ {
         named_region_map: named_region_map,
         item_variance_map: RefCell::new(HashMap::new()),
@@ -1126,7 +1127,7 @@ pub fn mk_ctxt(s: session::Session,
         upvar_borrow_map: RefCell::new(HashMap::new()),
         extern_const_statics: RefCell::new(HashMap::new()),
         extern_const_variants: RefCell::new(HashMap::new()),
-     }
+    }
 }
 
 // Type constructors
index 70b739e33d9b2078abf6bbf9f3c4b069f7f5f950..e569f0756e68263a58aaef6b7694c0123e6b0a2f 100644 (file)
@@ -51,7 +51,6 @@
 
 
 use middle::const_eval;
-use middle::lint;
 use middle::subst::Subst;
 use middle::ty::{substs};
 use middle::ty::{ty_param_substs_and_ty};
@@ -219,11 +218,12 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
                 expected, formal_ty_param_count, supplied_ty_param_count));
     }
 
-    if supplied_ty_param_count > required_ty_param_count {
-        let id = path.segments.iter().flat_map(|s| s.types.iter())
-                              .nth(required_ty_param_count).unwrap().id;
-        this.tcx().sess.add_lint(lint::DefaultTypeParamUsage, id, path.span,
-                                 ~"provided type arguments with defaults");
+    if supplied_ty_param_count > required_ty_param_count
+        && !this.tcx().sess.features.default_type_params.get() {
+        this.tcx().sess.span_err(path.span, "default type parameters are \
+                                             experimental and possibly buggy");
+        this.tcx().sess.span_note(path.span, "add #[feature(default_type_params)] \
+                                              to the crate attributes to enable");
     }
 
     let tps = path.segments.iter().flat_map(|s| s.types.iter())
index ac17d3271664bc5b04e5fd90fb13314558dcc535..5d89f2d2c68b25d0922cdd2d15e99adb37759ef5 100644 (file)
@@ -81,7 +81,6 @@
 use middle::lang_items::{ExchangeHeapLangItem, GcLangItem};
 use middle::lang_items::{ManagedHeapLangItem};
 use middle::lint::UnreachableCode;
-use middle::lint;
 use middle::pat_util::pat_id_map;
 use middle::pat_util;
 use middle::subst::Subst;
@@ -3750,9 +3749,12 @@ pub fn instantiate_path(fcx: @FnCtxt,
                   expected, user_ty_param_req, ty_substs_len));
         (fcx.infcx().next_ty_vars(ty_param_count), regions)
     } else {
-        if ty_substs_len > user_ty_param_req {
-            fcx.tcx().sess.add_lint(lint::DefaultTypeParamUsage, node_id, pth.span,
-                                    ~"provided type arguments with defaults");
+        if ty_substs_len > user_ty_param_req
+            && !fcx.tcx().sess.features.default_type_params.get() {
+            fcx.tcx().sess.span_err(pth.span, "default type parameters are \
+                                               experimental and possibly buggy");
+            fcx.tcx().sess.span_note(pth.span, "add #[feature(default_type_params)] \
+                                                to the crate attributes to enable");
         }
 
         // Build up the list of type parameters, inserting the self parameter
index d7fc85bc259eda6f6d5b13892158d85b406e3805..e96eedea5cbe82d744df0eedf35486c1ec8c5c64 100644 (file)
@@ -56,6 +56,8 @@
 
 // Turn on default type parameters.
 #[feature(default_type_params)];
+// NOTE remove the following two attributes after the next snapshot.
+#[allow(unrecognized_lint)];
 #[allow(default_type_param_usage)];
 
 // Don't link to std. We are std.
index 0b9158547ed83abd3f4e8f3d61685ff8b104c19d..acae4f9efa6160114a0dd663d2eb50fcebb9d621 100644 (file)
@@ -13,7 +13,6 @@
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
 use ext::deriving::generic::*;
-use parse::token::InternedString;
 
 pub fn expand_deriving_hash(cx: &mut ExtCtxt,
                             span: Span,
@@ -21,29 +20,18 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
                             item: @Item,
                             push: |@Item|) {
 
-    let allow_default_type_param_usage = cx.attribute(
-        span,
-        cx.meta_list(
-            span,
-            InternedString::new("allow"),
-            ~[cx.meta_word(span, InternedString::new("default_type_param_usage"))]));
-
     let hash_trait_def = TraitDef {
         span: span,
-        attributes: ~[allow_default_type_param_usage],
-        path: Path::new_(~["std", "hash", "Hash"], None,
-                         ~[~Literal(Path::new_local("__H"))], true),
+        attributes: ~[],
+        path: Path::new(~["std", "hash", "Hash"]),
         additional_bounds: ~[],
-        generics: LifetimeBounds {
-            lifetimes: ~[],
-            bounds: ~[("__H", ~[Path::new(~["std", "io", "Writer"])])],
-        },
+        generics: LifetimeBounds::empty(),
         methods: ~[
             MethodDef {
                 name: "hash",
                 generics: LifetimeBounds::empty(),
                 explicit_self: borrowed_explicit_self(),
-                args: ~[Ptr(~Literal(Path::new_local("__H")),
+                args: ~[Ptr(~Literal(Path::new(~["std", "hash", "sip", "SipState"])),
                             Borrowed(None, MutMutable))],
                 ret_ty: nil_ty(),
                 inline: true,
diff --git a/src/test/compile-fail/gated-default-type-param-usage.rs b/src/test/compile-fail/gated-default-type-param-usage.rs
new file mode 100644 (file)
index 0000000..9961a81
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:default_type_params_xc.rs
+
+#[deny(default_type_param_usage)];
+
+extern crate default_type_params_xc;
+
+pub struct FooAlloc;
+
+pub type VecFoo<T> = default_type_params_xc::FakeVec<T, FooAlloc>;
+//~^ ERROR: default type parameters are experimental
+
+fn main() {}
diff --git a/src/test/compile-fail/lint-default-type-param-usage.rs b/src/test/compile-fail/lint-default-type-param-usage.rs
deleted file mode 100644 (file)
index e8cacb0..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#[feature(default_type_params)];
-
-#[deny(default_type_param_usage)];
-
-pub struct Heap;
-
-pub struct Vec<T, A = Heap>;
-
-pub struct FooAlloc;
-
-pub type VecFoo<T> = Vec<T, FooAlloc>; //~ ERROR provided type arguments with defaults
-
-fn main() {}
index 412e780f8a23b51acf8a2155f5309f7fd1a2b697..72b416018910c57ede5cf2bdc513b2fe411a16b6 100644 (file)
@@ -13,8 +13,6 @@
 // ignore-fast #[feature] doesn't work with check-fast
 #[feature(default_type_params)];
 
-#[allow(default_type_param_usage)];
-
 extern crate default_type_params_xc;
 
 struct Vec<T, A = default_type_params_xc::Heap>;
index b2d20b859df2a72cfae1e78eef50bbd5274884e7..889d5c948eb1a0cc0f0575537a45536f17009012 100644 (file)
@@ -11,8 +11,6 @@
 // ignore-fast #[feature] doesn't work with check-fast
 #[feature(default_type_params)];
 
-#[allow(default_type_param_usage)];
-
 struct Foo<A = (int, char)> {
     a: A
 }