Skip to content

Commit bac3eec

Browse files
committed
[breaking-change] remove negate_unsigned feature gate
1 parent 69e1f57 commit bac3eec

11 files changed

+179
-90
lines changed

src/librustc_lint/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(staged_api)]
3939
#![feature(str_char)]
4040

41+
#[macro_use]
4142
extern crate syntax;
4243
#[macro_use]
4344
extern crate rustc;
@@ -153,4 +154,5 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
153154
store.register_renamed("unknown_features", "unused_features");
154155

155156
store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
157+
store.register_removed("negate_unsigned", "cast a signed value instead");
156158
}

src/librustc_lint/types.rs

+51-33
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+
#![allow(non_snake_case)]
12+
1113
use middle::{infer};
1214
use middle::def_id::DefId;
1315
use middle::subst::Substs;
@@ -24,13 +26,39 @@ use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
2426
use syntax::{abi, ast};
2527
use syntax::attr::{self, AttrMetaMethods};
2628
use syntax::codemap::{self, Span};
27-
use syntax::feature_gate::{emit_feature_err, GateIssue};
2829
use syntax::ast::{TyIs, TyUs, TyI8, TyU8, TyI16, TyU16, TyI32, TyU32, TyI64, TyU64};
2930

3031
use rustc_front::hir;
3132
use rustc_front::intravisit::{self, Visitor};
3233
use rustc_front::util::is_shift_binop;
3334

35+
register_long_diagnostics! {
36+
E0519: r##"
37+
It is not allowed to negate an unsigned integer.
38+
You can negate a signed integer and cast it to an
39+
unsigned integer or use the `!` operator.
40+
41+
```
42+
let x: usize = -1isize as usize;
43+
let y: usize = !0;
44+
assert_eq!(x, y);
45+
```
46+
47+
Alternatively you can use the `Wrapping` newtype
48+
or the `wrapping_neg` operation that all
49+
integral types support:
50+
51+
```
52+
use std::num::Wrapping;
53+
let x: Wrapping<usize> = -Wrapping(1);
54+
let Wrapping(x) = x;
55+
let y: usize = 1.wrapping_neg();
56+
assert_eq!(x, y);
57+
```
58+
59+
"##
60+
}
61+
3462
declare_lint! {
3563
UNUSED_COMPARISONS,
3664
Warn,
@@ -73,30 +101,24 @@ impl LateLintPass for TypeLimits {
73101
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
74102
match e.node {
75103
hir::ExprUnary(hir::UnNeg, ref expr) => {
76-
match expr.node {
77-
hir::ExprLit(ref lit) => {
78-
match lit.node {
79-
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
80-
check_unsigned_negation_feature(cx, e.span);
81-
},
82-
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
83-
if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
84-
check_unsigned_negation_feature(cx, e.span);
85-
}
86-
},
87-
_ => ()
88-
}
89-
},
90-
_ => {
91-
let t = cx.tcx.node_id_to_type(expr.id);
92-
match t.sty {
93-
ty::TyUint(_) => {
94-
check_unsigned_negation_feature(cx, e.span);
95-
},
96-
_ => ()
97-
}
104+
if let hir::ExprLit(ref lit) = expr.node {
105+
match lit.node {
106+
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
107+
forbid_unsigned_negation(cx, e.span);
108+
},
109+
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
110+
if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
111+
forbid_unsigned_negation(cx, e.span);
112+
}
113+
},
114+
_ => ()
98115
}
99-
};
116+
} else {
117+
let t = cx.tcx.node_id_to_type(expr.id);
118+
if let ty::TyUint(_) = t.sty {
119+
forbid_unsigned_negation(cx, e.span);
120+
}
121+
}
100122
// propagate negation, if the negation itself isn't negated
101123
if self.negated_expr_id != e.id {
102124
self.negated_expr_id = expr.id;
@@ -322,15 +344,11 @@ impl LateLintPass for TypeLimits {
322344
}
323345
}
324346

325-
fn check_unsigned_negation_feature(cx: &LateContext, span: Span) {
326-
if !cx.sess().features.borrow().negate_unsigned {
327-
emit_feature_err(
328-
&cx.sess().parse_sess.span_diagnostic,
329-
"negate_unsigned",
330-
span,
331-
GateIssue::Language,
332-
"unary negation of unsigned integers may be removed in the future");
333-
}
347+
fn forbid_unsigned_negation(cx: &LateContext, span: Span) {
348+
cx.sess()
349+
.struct_span_err_with_code(span, "unary negation of unsigned integer", "E0519")
350+
.span_help(span, "use a cast or the `!` operator")
351+
.emit();
334352
}
335353
}
336354
}

src/libsyntax/feature_gate.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
170170
("slice_patterns", "1.0.0", Some(23121), Active),
171171

172172
// Allows use of unary negate on unsigned integers, e.g. -e for e: u8
173-
("negate_unsigned", "1.0.0", Some(29645), Active),
173+
("negate_unsigned", "1.0.0", Some(29645), Removed),
174174

175175
// Allows the definition of associated constants in `trait` or `impl`
176176
// blocks.
@@ -548,7 +548,6 @@ pub struct Features {
548548
pub allow_pushpop_unsafe: bool,
549549
pub simd_ffi: bool,
550550
pub unmarked_api: bool,
551-
pub negate_unsigned: bool,
552551
/// spans of #![feature] attrs for stable language features. for error reporting
553552
pub declared_stable_lang_features: Vec<Span>,
554553
/// #![feature] attrs for non-language (library) features
@@ -585,7 +584,6 @@ impl Features {
585584
allow_pushpop_unsafe: false,
586585
simd_ffi: false,
587586
unmarked_api: false,
588-
negate_unsigned: false,
589587
declared_stable_lang_features: Vec::new(),
590588
declared_lib_features: Vec::new(),
591589
const_fn: false,
@@ -1174,7 +1172,6 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &Handler,
11741172
allow_pushpop_unsafe: cx.has_feature("pushpop_unsafe"),
11751173
simd_ffi: cx.has_feature("simd_ffi"),
11761174
unmarked_api: cx.has_feature("unmarked_api"),
1177-
negate_unsigned: cx.has_feature("negate_unsigned"),
11781175
declared_stable_lang_features: accepted_features,
11791176
declared_lib_features: unknown_features,
11801177
const_fn: cx.has_feature("const_fn"),

src/test/compile-fail/const-eval-overflow.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
#![allow(unused_imports)]
12-
#![feature(negate_unsigned)]
1312

1413
// Note: the relevant lint pass here runs before some of the constant
1514
// evaluation below (e.g. that performed by trans and llvm), so if you
@@ -65,7 +64,7 @@ const VALS_I64: (i64, i64, i64, i64) =
6564
);
6665

6766
const VALS_U8: (u8, u8, u8, u8) =
68-
(-u8::MIN,
67+
(-(u8::MIN as i8) as u8,
6968
u8::MIN - 1,
7069
//~^ ERROR attempted to sub with overflow
7170
u8::MAX + 1,
@@ -75,7 +74,7 @@ const VALS_U8: (u8, u8, u8, u8) =
7574
);
7675

7776
const VALS_U16: (u16, u16, u16, u16) =
78-
(-u16::MIN,
77+
(-(u16::MIN as i16) as u16,
7978
u16::MIN - 1,
8079
//~^ ERROR attempted to sub with overflow
8180
u16::MAX + 1,
@@ -85,7 +84,7 @@ const VALS_U16: (u16, u16, u16, u16) =
8584
);
8685

8786
const VALS_U32: (u32, u32, u32, u32) =
88-
(-u32::MIN,
87+
(-(u32::MIN as i32) as u32,
8988
u32::MIN - 1,
9089
//~^ ERROR attempted to sub with overflow
9190
u32::MAX + 1,
@@ -95,7 +94,7 @@ const VALS_U32: (u32, u32, u32, u32) =
9594
);
9695

9796
const VALS_U64: (u64, u64, u64, u64) =
98-
(-u64::MIN,
97+
(-(u64::MIN as i64) as u64,
9998
u64::MIN - 1,
10099
//~^ ERROR attempted to sub with overflow
101100
u64::MAX + 1,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#![allow(unused_imports)]
12+
13+
// Note: the relevant lint pass here runs before some of the constant
14+
// evaluation below (e.g. that performed by trans and llvm), so if you
15+
// change this warn to a deny, then the compiler will exit before
16+
// those errors are detected.
17+
18+
use std::fmt;
19+
use std::{i8, i16, i32, i64, isize};
20+
use std::{u8, u16, u32, u64, usize};
21+
22+
const VALS_I8: (i8, i8, i8, i8) =
23+
(-i8::MIN,
24+
i8::MIN - 1,
25+
i8::MAX + 1,
26+
i8::MIN * 2,
27+
);
28+
29+
const VALS_I16: (i16, i16, i16, i16) =
30+
(-i16::MIN,
31+
i16::MIN - 1,
32+
i16::MAX + 1,
33+
i16::MIN * 2,
34+
);
35+
36+
const VALS_I32: (i32, i32, i32, i32) =
37+
(-i32::MIN,
38+
i32::MIN - 1,
39+
i32::MAX + 1,
40+
i32::MIN * 2,
41+
);
42+
43+
const VALS_I64: (i64, i64, i64, i64) =
44+
(-i64::MIN,
45+
i64::MIN - 1,
46+
i64::MAX + 1,
47+
i64::MAX * 2,
48+
);
49+
50+
const VALS_U8: (u8, u8, u8, u8) =
51+
(-u8::MIN,
52+
//~^ ERROR unary negation of unsigned integer
53+
//~| HELP use a cast or the `!` operator
54+
u8::MIN - 1,
55+
u8::MAX + 1,
56+
u8::MAX * 2,
57+
);
58+
59+
const VALS_U16: (u16, u16, u16, u16) =
60+
(-u16::MIN,
61+
//~^ ERROR unary negation of unsigned integer
62+
//~| HELP use a cast or the `!` operator
63+
u16::MIN - 1,
64+
u16::MAX + 1,
65+
u16::MAX * 2,
66+
);
67+
68+
const VALS_U32: (u32, u32, u32, u32) =
69+
(-u32::MIN,
70+
//~^ ERROR unary negation of unsigned integer
71+
//~| HELP use a cast or the `!` operator
72+
u32::MIN - 1,
73+
u32::MAX + 1,
74+
u32::MAX * 2,
75+
);
76+
77+
const VALS_U64: (u64, u64, u64, u64) =
78+
(-u64::MIN,
79+
//~^ ERROR unary negation of unsigned integer
80+
//~| HELP use a cast or the `!` operator
81+
u64::MIN - 1,
82+
u64::MAX + 1,
83+
u64::MAX * 2,
84+
);
85+
86+
fn main() {
87+
foo(VALS_I8);
88+
foo(VALS_I16);
89+
foo(VALS_I32);
90+
foo(VALS_I64);
91+
92+
foo(VALS_U8);
93+
foo(VALS_U16);
94+
foo(VALS_U32);
95+
foo(VALS_U64);
96+
}
97+
98+
fn foo<T:fmt::Debug>(x: T) {
99+
println!("{:?}", x);
100+
}

src/test/compile-fail/enum-discrim-too-small.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(negate_unsigned)]
1211

1312
#[repr(u8)] //~ NOTE discriminant type specified here
1413
enum Eu8 {

src/test/compile-fail/feature-gate-negate-unsigned.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test that negating unsigned integers is gated by `negate_unsigned` feature
12-
// gate
11+
// Test that negating unsigned integers doesn't compile
1312

1413
struct S;
1514
impl std::ops::Neg for S {
@@ -18,21 +17,26 @@ impl std::ops::Neg for S {
1817
}
1918

2019
const _MAX: usize = -1;
21-
//~^ ERROR unary negation of unsigned integers may be removed in the future
20+
//~^ ERROR unary negation of unsigned integer
21+
//~| HELP use a cast or the `!` operator
2222

2323
fn main() {
2424
let a = -1;
25-
//~^ ERROR unary negation of unsigned integers may be removed in the future
25+
//~^ ERROR unary negation of unsigned integer
26+
//~| HELP use a cast or the `!` operator
2627
let _b : u8 = a; // for infering variable a to u8.
2728

2829
-a;
29-
//~^ ERROR unary negation of unsigned integers may be removed in the future
30+
//~^ ERROR unary negation of unsigned integer
31+
//~| HELP use a cast or the `!` operator
3032

3133
let _d = -1u8;
32-
//~^ ERROR unary negation of unsigned integers may be removed in the future
34+
//~^ ERROR unary negation of unsigned integer
35+
//~| HELP use a cast or the `!` operator
3336

3437
for _ in -10..10u8 {}
35-
//~^ ERROR unary negation of unsigned integers may be removed in the future
38+
//~^ ERROR unary negation of unsigned integer
39+
//~| HELP use a cast or the `!` operator
3640

3741
-S; // should not trigger the gate; issue 26840
3842
}

src/test/compile-fail/lint-type-limits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(negate_unsigned)]
1211
#![allow(dead_code)]
13-
#![feature(negate_unsigned)]
1412

1513
// compile-flags: -D unused-comparisons
1614
fn main() { }

src/test/run-pass/bitwise.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(negate_unsigned)]
12-
1311
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
1412
fn target() {
15-
assert_eq!(-1000 as usize >> 3_usize, 536870787_usize);
13+
assert_eq!(-1000isize as usize >> 3_usize, 536870787_usize);
1614
}
1715

1816
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
1917
fn target() {
20-
assert_eq!(-1000 as usize >> 3_usize, 2305843009213693827_usize);
18+
assert_eq!(-1000isize as usize >> 3_usize, 2305843009213693827_usize);
2119
}
2220

2321
fn general() {

0 commit comments

Comments
 (0)