]> git.lizzy.rs Git - rust.git/commitdiff
Turn the duplicate feature lint into an error
authorvarkor <github@varkor.com>
Mon, 23 Jul 2018 16:34:04 +0000 (17:34 +0100)
committervarkor <github@varkor.com>
Sun, 5 Aug 2018 14:54:49 +0000 (15:54 +0100)
src/librustc/diagnostics.rs
src/librustc/lint/builtin.rs
src/librustc/middle/stability.rs
src/librustc_lint/lib.rs
src/test/ui/feature-gate/duplicate-features.rs
src/test/ui/feature-gate/duplicate-features.stderr

index 3b3be7578822bdd7b2e98141b45138f4c76aaa57..b78cfc9499f398be5ed1e380d6dc3341684e9b24 100644 (file)
@@ -1918,6 +1918,19 @@ fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
 ```
 "##,
 
+E0636: r##"
+A `#![feature]` attribute was declared multiple times.
+
+Erroneous code example:
+
+```compile_fail,E0636
+#![allow(stable_features)]
+#![feature(rust1)]
+#![feature(rust1)] // error: the feature `rust1` has already been declared
+```
+
+"##,
+
 E0644: r##"
 A closure or generator was constructed that references its own type.
 
index 2301055e5cec2a509105d617d5673ac55006bd32..6536ab6ea73b3dbcbd3981b45130209bd5f5a302 100644 (file)
     "unknown features found in crate-level #[feature] directives"
 }
 
-declare_lint! {
-    pub DUPLICATE_FEATURES,
-    Deny,
-    "duplicate features found in crate-level #[feature] directives"
-}
-
 declare_lint! {
     pub STABLE_FEATURES,
     Warn,
@@ -375,7 +369,6 @@ fn get_lints(&self) -> LintArray {
             WARNINGS,
             UNUSED_FEATURES,
             UNKNOWN_FEATURES,
-            DUPLICATE_FEATURES,
             STABLE_FEATURES,
             UNKNOWN_CRATE_TYPES,
             TRIVIAL_CASTS,
index 85d4bcbd0d4f018d7db9d10c35ef7ed7cb37d02f..bbf03801fdb319e4419e7b76b6c65bfc87afafeb 100644 (file)
@@ -18,7 +18,7 @@
 use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
 use ty::{self, TyCtxt};
 use middle::privacy::AccessLevels;
-use session::DiagnosticMessageId;
+use session::{DiagnosticMessageId, Session};
 use syntax::symbol::Symbol;
 use syntax_pos::{Span, MultiSpan};
 use syntax::ast;
@@ -816,20 +816,14 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
 
     let declared_lang_features = &tcx.features().declared_lang_features;
     let mut lang_features = FxHashSet();
-    for &(ref feature, span, since) in declared_lang_features {
+    for &(feature, span, since) in declared_lang_features {
         if let Some(since) = since {
             // Warn if the user has enabled an already-stable lang feature.
-            tcx.lint_node(lint::builtin::STABLE_FEATURES,
-                        ast::CRATE_NODE_ID,
-                        span,
-                        &format_stable_since_msg(*feature, since));
+            unnecessary_stable_feature_lint(tcx, span, feature, since);
         }
         if lang_features.contains(&feature) {
             // Warn if the user enables a lang feature multiple times.
-            tcx.lint_node(lint::builtin::DUPLICATE_FEATURES,
-                          ast::CRATE_NODE_ID,
-                          span,
-                          &format!("duplicate `{}` feature attribute", feature));
+            duplicate_feature_err(tcx.sess, span, feature);
         }
         lang_features.insert(feature);
     }
@@ -837,12 +831,9 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
     let declared_lib_features = &tcx.features().declared_lib_features;
     let mut remaining_lib_features = FxHashMap();
     for (feature, span) in declared_lib_features {
-        // Warn if the user enables a lib feature multiple times.
         if remaining_lib_features.contains_key(&feature) {
-            tcx.lint_node(lint::builtin::DUPLICATE_FEATURES,
-                          ast::CRATE_NODE_ID,
-                          *span,
-                          &format!("duplicate `{}` feature attribute", feature));
+            // Warn if the user enables a lib feature multiple times.
+            duplicate_feature_err(tcx.sess, *span, *feature);
         }
         remaining_lib_features.insert(feature, span.clone());
     }
@@ -851,16 +842,12 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
     remaining_lib_features.remove(&Symbol::intern("libc"));
 
     for (feature, stable) in tcx.lib_features().iter() {
-        // Warn if the user has enabled an already-stable lib feature.
         if let Some(since) = stable {
             if let Some(span) = remaining_lib_features.get(&feature) {
-                tcx.lint_node(lint::builtin::STABLE_FEATURES,
-                              ast::CRATE_NODE_ID,
-                              *span,
-                              &format_stable_since_msg(feature, since));
+                // Warn if the user has enabled an already-stable lib feature.
+                unnecessary_stable_feature_lint(tcx, *span, feature, since);
             }
         }
-
         remaining_lib_features.remove(&feature);
     }
 
@@ -875,8 +862,20 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
     // don't lint about unused features. We should reenable this one day!
 }
 
-fn format_stable_since_msg(feature: Symbol, since: Symbol) -> String {
-    // "this feature has been stable since {}. Attribute no longer needed"
-    format!("the feature `{}` has been stable since {} and no longer requires \
-             an attribute to enable", feature, since)
+fn unnecessary_stable_feature_lint<'a, 'tcx>(
+    tcx: TyCtxt<'a, 'tcx, 'tcx>,
+    span: Span,
+    feature: Symbol,
+    since: Symbol
+) {
+    tcx.lint_node(lint::builtin::STABLE_FEATURES,
+        ast::CRATE_NODE_ID,
+        span,
+        &format!("the feature `{}` has been stable since {} and no longer requires \
+                  an attribute to enable", feature, since));
+}
+
+fn duplicate_feature_err(sess: &Session, span: Span, feature: Symbol) {
+    struct_span_err!(sess, span, E0636, "the feature `{}` has already been declared", feature)
+        .emit();
 }
index 4d8cee294e36e299ff4034689d6a989f350dc1ae..396e5e869f30738c9e1acda49eb08ea215b78b8c 100644 (file)
@@ -189,7 +189,6 @@ macro_rules! add_lint_group {
                     UNUSED_EXTERN_CRATES,
                     UNUSED_FEATURES,
                     UNKNOWN_FEATURES,
-                    DUPLICATE_FEATURES,
                     UNUSED_LABELS,
                     UNUSED_PARENS);
 
index 90d2a6cebcce6e0a68f52067e49a138cad284ac5..163a28772a59eaf7f3e145a49e3808a28a98af91 100644 (file)
@@ -9,12 +9,11 @@
 // except according to those terms.
 
 #![allow(stable_features)]
-#![deny(duplicate_features)]
 
 #![feature(rust1)]
-#![feature(rust1)] //~ ERROR duplicate `rust1` feature attribute
+#![feature(rust1)] //~ ERROR the feature `rust1` has already been declared
 
 #![feature(if_let)]
-#![feature(if_let)] //~ ERROR duplicate `if_let` feature attribute
+#![feature(if_let)] //~ ERROR the feature `if_let` has already been declared
 
 fn main() {}
index bf8e9157db08fa8755e23dd52639e09de47e8287..d55297bdd0e7be2fcba4e1fc442ef79248c90fc7 100644 (file)
@@ -1,20 +1,15 @@
-error: duplicate `if_let` feature attribute
-  --> $DIR/duplicate-features.rs:18:12
+error[E0636]: the feature `if_let` has already been declared
+  --> $DIR/duplicate-features.rs:17:12
    |
-LL | #![feature(if_let)] //~ ERROR duplicate `if_let` feature attribute
+LL | #![feature(if_let)] //~ ERROR the feature `if_let` has already been declared
    |            ^^^^^^
-   |
-note: lint level defined here
-  --> $DIR/duplicate-features.rs:12:9
-   |
-LL | #![deny(duplicate_features)]
-   |         ^^^^^^^^^^^^^^^^^^
 
-error: duplicate `rust1` feature attribute
-  --> $DIR/duplicate-features.rs:15:12
+error[E0636]: the feature `rust1` has already been declared
+  --> $DIR/duplicate-features.rs:14:12
    |
-LL | #![feature(rust1)] //~ ERROR duplicate `rust1` feature attribute
+LL | #![feature(rust1)] //~ ERROR the feature `rust1` has already been declared
    |            ^^^^^
 
 error: aborting due to 2 previous errors
 
+For more information about this error, try `rustc --explain E0636`.