Skip to content

Commit 47619c0

Browse files
committed
Turn the duplicate feature lint into an error
1 parent c81b95f commit 47619c0

File tree

6 files changed

+46
-48
lines changed

6 files changed

+46
-48
lines changed

src/librustc/diagnostics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,19 @@ fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
19181918
```
19191919
"##,
19201920

1921+
E0636: r##"
1922+
A `#![feature]` attribute was declared multiple times.
1923+
1924+
Erroneous code example:
1925+
1926+
```compile_fail,E0636
1927+
#![allow(stable_features)]
1928+
#![feature(rust1)]
1929+
#![feature(rust1)] // error: the feature `rust1` has already been declared
1930+
```
1931+
1932+
"##,
1933+
19211934
E0644: r##"
19221935
A closure or generator was constructed that references its own type.
19231936

src/librustc/lint/builtin.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ declare_lint! {
111111
"unknown features found in crate-level #[feature] directives"
112112
}
113113

114-
declare_lint! {
115-
pub DUPLICATE_FEATURES,
116-
Deny,
117-
"duplicate features found in crate-level #[feature] directives"
118-
}
119-
120114
declare_lint! {
121115
pub STABLE_FEATURES,
122116
Warn,
@@ -375,7 +369,6 @@ impl LintPass for HardwiredLints {
375369
WARNINGS,
376370
UNUSED_FEATURES,
377371
UNKNOWN_FEATURES,
378-
DUPLICATE_FEATURES,
379372
STABLE_FEATURES,
380373
UNKNOWN_CRATE_TYPES,
381374
TRIVIAL_CASTS,

src/librustc/middle/stability.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hir::def::Def;
1818
use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
1919
use ty::{self, TyCtxt};
2020
use middle::privacy::AccessLevels;
21-
use session::DiagnosticMessageId;
21+
use session::{DiagnosticMessageId, Session};
2222
use syntax::symbol::Symbol;
2323
use syntax_pos::{Span, MultiSpan};
2424
use syntax::ast;
@@ -816,33 +816,24 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
816816

817817
let declared_lang_features = &tcx.features().declared_lang_features;
818818
let mut lang_features = FxHashSet();
819-
for &(ref feature, span, since) in declared_lang_features {
819+
for &(feature, span, since) in declared_lang_features {
820820
if let Some(since) = since {
821821
// Warn if the user has enabled an already-stable lang feature.
822-
tcx.lint_node(lint::builtin::STABLE_FEATURES,
823-
ast::CRATE_NODE_ID,
824-
span,
825-
&format_stable_since_msg(*feature, since));
822+
unnecessary_stable_feature_lint(tcx, span, feature, since);
826823
}
827824
if lang_features.contains(&feature) {
828825
// Warn if the user enables a lang feature multiple times.
829-
tcx.lint_node(lint::builtin::DUPLICATE_FEATURES,
830-
ast::CRATE_NODE_ID,
831-
span,
832-
&format!("duplicate `{}` feature attribute", feature));
826+
duplicate_feature_err(tcx.sess, span, feature);
833827
}
834828
lang_features.insert(feature);
835829
}
836830

837831
let declared_lib_features = &tcx.features().declared_lib_features;
838832
let mut remaining_lib_features = FxHashMap();
839833
for (feature, span) in declared_lib_features {
840-
// Warn if the user enables a lib feature multiple times.
841834
if remaining_lib_features.contains_key(&feature) {
842-
tcx.lint_node(lint::builtin::DUPLICATE_FEATURES,
843-
ast::CRATE_NODE_ID,
844-
*span,
845-
&format!("duplicate `{}` feature attribute", feature));
835+
// Warn if the user enables a lib feature multiple times.
836+
duplicate_feature_err(tcx.sess, *span, *feature);
846837
}
847838
remaining_lib_features.insert(feature, span.clone());
848839
}
@@ -851,16 +842,12 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
851842
remaining_lib_features.remove(&Symbol::intern("libc"));
852843

853844
for (feature, stable) in tcx.lib_features().iter() {
854-
// Warn if the user has enabled an already-stable lib feature.
855845
if let Some(since) = stable {
856846
if let Some(span) = remaining_lib_features.get(&feature) {
857-
tcx.lint_node(lint::builtin::STABLE_FEATURES,
858-
ast::CRATE_NODE_ID,
859-
*span,
860-
&format_stable_since_msg(feature, since));
847+
// Warn if the user has enabled an already-stable lib feature.
848+
unnecessary_stable_feature_lint(tcx, *span, feature, since);
861849
}
862850
}
863-
864851
remaining_lib_features.remove(&feature);
865852
}
866853

@@ -875,8 +862,20 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
875862
// don't lint about unused features. We should reenable this one day!
876863
}
877864

878-
fn format_stable_since_msg(feature: Symbol, since: Symbol) -> String {
879-
// "this feature has been stable since {}. Attribute no longer needed"
880-
format!("the feature `{}` has been stable since {} and no longer requires \
881-
an attribute to enable", feature, since)
865+
fn unnecessary_stable_feature_lint<'a, 'tcx>(
866+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
867+
span: Span,
868+
feature: Symbol,
869+
since: Symbol
870+
) {
871+
tcx.lint_node(lint::builtin::STABLE_FEATURES,
872+
ast::CRATE_NODE_ID,
873+
span,
874+
&format!("the feature `{}` has been stable since {} and no longer requires \
875+
an attribute to enable", feature, since));
876+
}
877+
878+
fn duplicate_feature_err(sess: &Session, span: Span, feature: Symbol) {
879+
struct_span_err!(sess, span, E0636, "the feature `{}` has already been declared", feature)
880+
.emit();
882881
}

src/librustc_lint/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
189189
UNUSED_EXTERN_CRATES,
190190
UNUSED_FEATURES,
191191
UNKNOWN_FEATURES,
192-
DUPLICATE_FEATURES,
193192
UNUSED_LABELS,
194193
UNUSED_PARENS);
195194

src/test/ui/feature-gate/duplicate-features.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
// except according to those terms.
1010

1111
#![allow(stable_features)]
12-
#![deny(duplicate_features)]
1312

1413
#![feature(rust1)]
15-
#![feature(rust1)] //~ ERROR duplicate `rust1` feature attribute
14+
#![feature(rust1)] //~ ERROR the feature `rust1` has already been declared
1615

1716
#![feature(if_let)]
18-
#![feature(if_let)] //~ ERROR duplicate `if_let` feature attribute
17+
#![feature(if_let)] //~ ERROR the feature `if_let` has already been declared
1918

2019
fn main() {}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
error: duplicate `if_let` feature attribute
2-
--> $DIR/duplicate-features.rs:18:12
1+
error[E0636]: the feature `if_let` has already been declared
2+
--> $DIR/duplicate-features.rs:17:12
33
|
4-
LL | #![feature(if_let)] //~ ERROR duplicate `if_let` feature attribute
4+
LL | #![feature(if_let)] //~ ERROR the feature `if_let` has already been declared
55
| ^^^^^^
6-
|
7-
note: lint level defined here
8-
--> $DIR/duplicate-features.rs:12:9
9-
|
10-
LL | #![deny(duplicate_features)]
11-
| ^^^^^^^^^^^^^^^^^^
126

13-
error: duplicate `rust1` feature attribute
14-
--> $DIR/duplicate-features.rs:15:12
7+
error[E0636]: the feature `rust1` has already been declared
8+
--> $DIR/duplicate-features.rs:14:12
159
|
16-
LL | #![feature(rust1)] //~ ERROR duplicate `rust1` feature attribute
10+
LL | #![feature(rust1)] //~ ERROR the feature `rust1` has already been declared
1711
| ^^^^^
1812

1913
error: aborting due to 2 previous errors
2014

15+
For more information about this error, try `rustc --explain E0636`.

0 commit comments

Comments
 (0)