From d12514bc589c1955108d517acd6d5952929b1650 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 8 Jan 2015 21:16:35 +1100 Subject: Add a warning feature gate for int/uint in types and i/u suffixes. --- src/libsyntax/feature_gate.rs | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/libsyntax/feature_gate.rs') diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 2cfcd38d48f..6deffa804b2 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -93,6 +93,9 @@ static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[ // OIBIT specific features ("optin_builtin_traits", Active), + // int and uint are now deprecated + ("int_uint", Active), + // These are used to test this portion of the compiler, they don't actually // mean anything ("test_accepted_feature", Accepted), @@ -157,6 +160,14 @@ impl<'a> Context<'a> { } } + fn warn_feature(&self, feature: &str, span: Span, explain: &str) { + if !self.has_feature(feature) { + self.span_handler.span_warn(span, explain); + self.span_handler.span_help(span, &format!("add #![feature({})] to the \ + crate attributes to silence this warning", + feature)[]); + } + } fn has_feature(&self, feature: &str) -> bool { self.features.iter().any(|&n| n == feature) } @@ -334,6 +345,31 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { } fn visit_ty(&mut self, t: &ast::Ty) { + match t.node { + ast::TyPath(ref p, _) => { + match &*p.segments { + + [ast::PathSegment { identifier, .. }] => { + let name = token::get_ident(identifier); + let msg = if name == "int" { + Some("the `int` type is deprecated; \ + use `isize` or a fixed-sized integer") + } else if name == "uint" { + Some("the `unt` type is deprecated; \ + use `usize` or a fixed-sized integer") + } else { + None + }; + + if let Some(msg) = msg { + self.context.warn_feature("int_uint", t.span, msg) + } + } + _ => {} + } + } + _ => {} + } visit::walk_ty(self, t); } @@ -345,6 +381,25 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { "box expression syntax is experimental in alpha release; \ you can call `Box::new` instead."); } + ast::ExprLit(ref lit) => { + match lit.node { + ast::LitInt(_, ty) => { + let msg = if let ast::SignedIntLit(ast::TyIs(true), _) = ty { + Some("the `i` suffix on integers is deprecated; use `is` \ + or one of the fixed-sized suffixes") + } else if let ast::UnsignedIntLit(ast::TyUs(true)) = ty { + Some("the `u` suffix on integers is deprecated; use `us` \ + or one of the fixed-sized suffixes") + } else { + None + }; + if let Some(msg) = msg { + self.context.warn_feature("int_uint", e.span, msg); + } + } + _ => {} + } + } _ => {} } visit::walk_expr(self, e); -- cgit 1.4.1-3-g733a5 From dc1ba08d1603aeedf37a4a7182e990207891379d Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 8 Jan 2015 23:36:24 +1100 Subject: Test fixes. --- src/libsyntax/ext/deriving/rand.rs | 6 +++--- src/libsyntax/feature_gate.rs | 2 +- src/test/compile-fail/feature-gate-int-uint.rs | 16 ++++++++-------- src/test/compile-fail/issue-13482-2.rs | 2 +- src/test/compile-fail/issue-5544-a.rs | 2 +- src/test/compile-fail/lex-bad-numeric-literals.rs | 4 ++-- src/test/compile-fail/lint-type-limits.rs | 4 ++-- src/test/compile-fail/oversized-literal.rs | 2 +- 8 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src/libsyntax/feature_gate.rs') diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index 5517019f804..1359cada673 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -98,13 +98,13 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) rand_name, vec!(rng.clone())); - // need to specify the uint-ness of the random number - let uint_ty = cx.ty_ident(trait_span, cx.ident_of("uint")); + // need to specify the usize-ness of the random number + let usize_ty = cx.ty_ident(trait_span, cx.ident_of("usize")); let value_ident = cx.ident_of("__value"); let let_statement = cx.stmt_let_typed(trait_span, false, value_ident, - uint_ty, + usize_ty, rv_call); // rand() % variants.len() diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 6deffa804b2..ec1910f9017 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -355,7 +355,7 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { Some("the `int` type is deprecated; \ use `isize` or a fixed-sized integer") } else if name == "uint" { - Some("the `unt` type is deprecated; \ + Some("the `uint` type is deprecated; \ use `usize` or a fixed-sized integer") } else { None diff --git a/src/test/compile-fail/feature-gate-int-uint.rs b/src/test/compile-fail/feature-gate-int-uint.rs index 94190cc1511..016a0394289 100644 --- a/src/test/compile-fail/feature-gate-int-uint.rs +++ b/src/test/compile-fail/feature-gate-int-uint.rs @@ -11,21 +11,21 @@ #![allow(dead_code)] mod u { - type X = usize; //~ WARN the `usize` type is deprecated + type X = uint; //~ WARN the `uint` type is deprecated struct Foo { - x: usize //~ WARN the `usize` type is deprecated + x: uint //~ WARN the `uint` type is deprecated } - fn bar(x: usize) { //~ WARN the `usize` type is deprecated - 1us; //~ WARN the `u` suffix on integers is deprecated + fn bar(x: uint) { //~ WARN the `uint` type is deprecated + 1u; //~ WARN the `u` suffix on integers is deprecated } } mod i { - type X = isize; //~ WARN the `isize` type is deprecated + type X = int; //~ WARN the `int` type is deprecated struct Foo { - x: isize //~ WARN the `isize` type is deprecated + x: int //~ WARN the `int` type is deprecated } - fn bar(x: isize) { //~ WARN the `isize` type is deprecated - 1is; //~ WARN the `u` suffix on integers is deprecated + fn bar(x: int) { //~ WARN the `int` type is deprecated + 1i; //~ WARN the `i` suffix on integers is deprecated } } diff --git a/src/test/compile-fail/issue-13482-2.rs b/src/test/compile-fail/issue-13482-2.rs index 5c9b0473cea..ef7d3d4d158 100644 --- a/src/test/compile-fail/issue-13482-2.rs +++ b/src/test/compile-fail/issue-13482-2.rs @@ -14,7 +14,7 @@ fn main() { let x = [1,2]; let y = match x { [] => None, - //~^ ERROR types: expected `[_#0is; 2]`, found `[_#7t; 0]` + //~^ ERROR types: expected `[_#0i; 2]`, found `[_#7t; 0]` // (expected array of 2 elements, found array of 0 elements) [a,_] => Some(a) }; diff --git a/src/test/compile-fail/issue-5544-a.rs b/src/test/compile-fail/issue-5544-a.rs index 6db126f403a..42a18ba5fb7 100644 --- a/src/test/compile-fail/issue-5544-a.rs +++ b/src/test/compile-fail/issue-5544-a.rs @@ -10,5 +10,5 @@ fn main() { let _i = 18446744073709551616; // 2^64 - //~^ ERROR isize literal is too large + //~^ ERROR int literal is too large } diff --git a/src/test/compile-fail/lex-bad-numeric-literals.rs b/src/test/compile-fail/lex-bad-numeric-literals.rs index 273a7627d73..9a490be6a01 100644 --- a/src/test/compile-fail/lex-bad-numeric-literals.rs +++ b/src/test/compile-fail/lex-bad-numeric-literals.rs @@ -21,8 +21,8 @@ fn main() { 0o; //~ ERROR: no valid digits 1e+; //~ ERROR: expected at least one digit in exponent 0x539.0; //~ ERROR: hexadecimal float literal is not supported - 99999999999999999999999999999999; //~ ERROR: isize literal is too large - 99999999999999999999999999999999u32; //~ ERROR: isize literal is too large + 99999999999999999999999999999999; //~ ERROR: int literal is too large + 99999999999999999999999999999999u32; //~ ERROR: int literal is too large 0x; //~ ERROR: no valid digits 0xu32; //~ ERROR: no valid digits 0ou32; //~ ERROR: no valid digits diff --git a/src/test/compile-fail/lint-type-limits.rs b/src/test/compile-fail/lint-type-limits.rs index 3224dec1c99..a2bc464ac49 100644 --- a/src/test/compile-fail/lint-type-limits.rs +++ b/src/test/compile-fail/lint-type-limits.rs @@ -50,12 +50,12 @@ fn qux() { } fn quy() { - let i = -23us; //~ WARNING negation of unsigned isize literal may be unintentional + let i = -23us; //~ WARNING negation of unsigned int literal may be unintentional //~^ WARNING unused variable } fn quz() { let i = 23us; - let j = -i; //~ WARNING negation of unsigned isize variable may be unintentional + let j = -i; //~ WARNING negation of unsigned int variable may be unintentional //~^ WARNING unused variable } diff --git a/src/test/compile-fail/oversized-literal.rs b/src/test/compile-fail/oversized-literal.rs index 2a70653bd6c..5416bcacf3d 100644 --- a/src/test/compile-fail/oversized-literal.rs +++ b/src/test/compile-fail/oversized-literal.rs @@ -9,5 +9,5 @@ // except according to those terms. fn main() { - println!("{}", 18446744073709551616u64); //~ error: isize literal is too large + println!("{}", 18446744073709551616u64); //~ error: int literal is too large } -- cgit 1.4.1-3-g733a5