]> git.lizzy.rs Git - rust.git/commitdiff
Add a warning feature gate for int/uint in types and i/u suffixes.
authorHuon Wilson <dbau.pp+github@gmail.com>
Thu, 8 Jan 2015 10:16:35 +0000 (21:16 +1100)
committerNiko Matsakis <niko@alum.mit.edu>
Thu, 8 Jan 2015 16:02:23 +0000 (11:02 -0500)
src/librustc/middle/ty.rs
src/librustc_trans/trans/type_of.rs
src/libsyntax/feature_gate.rs
src/test/compile-fail/feature-gate-int-uint.rs [new file with mode: 0644]

index 2534232960fad5b22e3a45bf9d4e239b8eb673e2..ef86e67de1606af77d7c0e3397fb7309412f1f0f 100644 (file)
@@ -2341,12 +2341,12 @@ fn new(arena: &'tcx TypedArena<TyS<'tcx>>,
             bool: intern_ty(arena, interner, ty_bool),
             char: intern_ty(arena, interner, ty_char),
             err: intern_ty(arena, interner, ty_err),
-            int: intern_ty(arena, interner, ty_int(ast::TyIs(_))),
+            int: intern_ty(arena, interner, ty_int(ast::TyIs(false))),
             i8: intern_ty(arena, interner, ty_int(ast::TyI8)),
             i16: intern_ty(arena, interner, ty_int(ast::TyI16)),
             i32: intern_ty(arena, interner, ty_int(ast::TyI32)),
             i64: intern_ty(arena, interner, ty_int(ast::TyI64)),
-            uint: intern_ty(arena, interner, ty_uint(ast::TyUs(_))),
+            uint: intern_ty(arena, interner, ty_uint(ast::TyUs(false))),
             u8: intern_ty(arena, interner, ty_uint(ast::TyU8)),
             u16: intern_ty(arena, interner, ty_uint(ast::TyU16)),
             u32: intern_ty(arena, interner, ty_uint(ast::TyU32)),
index 416dfb420ffae0068d301998d96fc5b2426d1ee7..99330797422299b817bbdc9521f663151ce13e88 100644 (file)
@@ -263,7 +263,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty
         }
 
         match unsized_part_of_type(cx.tcx(), t).sty {
-            ty::ty_str | ty::ty_vec(..) => Type::uint_from_ty(cx, ast::TyUs(_)),
+            ty::ty_str | ty::ty_vec(..) => Type::uint_from_ty(cx, ast::TyUs(false)),
             ty::ty_trait(_) => Type::vtable_ptr(cx),
             _ => panic!("Unexpected type returned from unsized_part_of_type : {}",
                        t.repr(cx.tcx()))
index 2cfcd38d48fca9d4be9034fb232dd48bbfbb7dd8..6deffa804b2df40ac5f6069eb1b93395a657ff92 100644 (file)
@@ -93,6 +93,9 @@
     // 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 @@ fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
         }
     }
 
+    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 @@ fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
     }
 
     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 @@ fn visit_expr(&mut self, e: &ast::Expr) {
                                   "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);
diff --git a/src/test/compile-fail/feature-gate-int-uint.rs b/src/test/compile-fail/feature-gate-int-uint.rs
new file mode 100644 (file)
index 0000000..78b931b
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2015 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.
+
+#![allow(dead_code)]
+
+mod u {
+    type X = uint; //~ WARN the `uint` type is deprecated
+    struct Foo {
+        x: uint //~ WARN the `uint` type 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 = int; //~ WARN the `int` type is deprecated
+    struct Foo {
+        x: int //~ WARN the `int` type is deprecated
+    }
+    fn bar(x: int) { //~ WARN the `int` type is deprecated
+        1i; //~ WARN the `u` suffix on integers is deprecated
+    }
+}
+
+fn main() {
+    // make compilation fail, after feature gating
+    let () = 1u8; //~ ERROR
+}