]> git.lizzy.rs Git - rust.git/commitdiff
do not print wrapping ranges like normal ranges in diagnostics
authorRalf Jung <post@ralfj.de>
Sat, 3 Nov 2018 11:44:10 +0000 (12:44 +0100)
committerRalf Jung <post@ralfj.de>
Wed, 7 Nov 2018 12:46:49 +0000 (13:46 +0100)
src/librustc_mir/interpret/validity.rs
src/test/ui/consts/const-eval/ub-enum.rs
src/test/ui/consts/const-eval/ub-enum.stderr

index 8c8b3e2ca77c49ddea6244d1ce8318a12a25a195..7efeaee6cce0a142933620bed8095060f3ec161a 100644 (file)
@@ -10,6 +10,7 @@
 
 use std::fmt::Write;
 use std::hash::Hash;
+use std::ops::RangeInclusive;
 
 use syntax_pos::symbol::Symbol;
 use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf};
@@ -122,6 +123,34 @@ fn path_format(path: &Vec<PathElem>) -> String {
     out
 }
 
+// Test if a range that wraps at overflow contains `test`
+fn wrapping_range_contains(r: &RangeInclusive<u128>, test: u128) -> bool {
+    let (lo, hi) = r.clone().into_inner();
+    if lo > hi {
+        // Wrapped
+        (..=hi).contains(&test) || (lo..).contains(&test)
+    } else {
+        // Normal
+        r.contains(&test)
+    }
+}
+
+// Formats such that a sentence like "expected something {}" to mean
+// "expected something <in the given range>" makes sense.
+fn wrapping_range_format(r: &RangeInclusive<u128>, max_hi: u128) -> String {
+    let (lo, hi) = r.clone().into_inner();
+    debug_assert!(hi <= max_hi);
+    if lo > hi {
+        format!("less or equal to {}, or greater or equal to {}", hi, lo)
+    } else {
+        if hi == max_hi {
+            format!("greater or equal to {}", lo)
+        } else {
+            format!("in the range {:?}", r)
+        }
+    }
+}
+
 struct ValidityVisitor<'rt, 'a: 'rt, 'mir: 'rt, 'tcx: 'a+'rt+'mir, M: Machine<'a, 'mir, 'tcx>+'rt> {
     /// The `path` may be pushed to, but the part that is present when a function
     /// starts must not be changed!  `visit_fields` and `visit_array` rely on
@@ -428,8 +457,8 @@ fn visit_scalar(
                         "a pointer",
                         self.path,
                         format!(
-                            "something that cannot possibly be outside the (wrapping) range {:?}",
-                            layout.valid_range
+                            "something that cannot possibly fail to be {}",
+                            wrapping_range_format(&layout.valid_range, max_hi)
                         )
                     );
                 }
@@ -440,33 +469,14 @@ fn visit_scalar(
             }
         };
         // Now compare. This is slightly subtle because this is a special "wrap-around" range.
-        use std::ops::RangeInclusive;
-        let in_range = |bound: RangeInclusive<u128>| bound.contains(&bits);
-        if lo > hi {
-            // wrapping around
-            if in_range(0..=hi) || in_range(lo..=max_hi) {
-                Ok(())
-            } else {
-                validation_failure!(
-                    bits,
-                    self.path,
-                    format!("something in the range {:?} or {:?}", 0..=hi, lo..=max_hi)
-                )
-            }
+        if wrapping_range_contains(&layout.valid_range, bits) {
+            Ok(())
         } else {
-            if in_range(layout.valid_range.clone()) {
-                Ok(())
-            } else {
-                validation_failure!(
-                    bits,
-                    self.path,
-                    if hi == max_hi {
-                        format!("something greater or equal to {}", lo)
-                    } else {
-                        format!("something in the range {:?}", layout.valid_range)
-                    }
-                )
-            }
+            validation_failure!(
+                bits,
+                self.path,
+                format!("something {}", wrapping_range_format(&layout.valid_range, max_hi))
+            )
         }
     }
 
index 309627581d0512cd5a881ceb013b7f03cdb7d306..0aa15c839387952f70474057d56d400c02cf0577 100644 (file)
@@ -17,39 +17,48 @@ enum Enum {
 }
 union TransmuteEnum {
     a: &'static u8,
-    b: Enum,
+    out: Enum,
 }
 
 // A pointer is guaranteed non-null
-const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.b };
+const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.out };
 //~^ ERROR is undefined behavior
 
-// Invalid enum discriminant
+// (Potentially) invalid enum discriminant
 #[repr(usize)]
 #[derive(Copy, Clone)]
 enum Enum2 {
     A = 2,
 }
+#[repr(transparent)]
+#[derive(Copy, Clone)]
+struct Wrap<T>(T);
 union TransmuteEnum2 {
-    a: usize,
-    b: Enum2,
-    c: (),
+    in1: usize,
+    in2: &'static u8,
+    in3: (),
+    out1: Enum2,
+    out2: Wrap<Enum2>, // something wrapping the enum so that we test layout first, not enum
 }
-const BAD_ENUM2 : Enum2 = unsafe { TransmuteEnum2 { a: 0 }.b };
+const BAD_ENUM2: Enum2 = unsafe { TransmuteEnum2 { in1: 0 }.out1 };
+//~^ ERROR is undefined behavior
+const BAD_ENUM3: Enum2 = unsafe { TransmuteEnum2 { in2: &0 }.out1 };
+//~^ ERROR is undefined behavior
+const BAD_ENUM4: Wrap<Enum2> = unsafe { TransmuteEnum2 { in2: &0 }.out2 };
 //~^ ERROR is undefined behavior
 
 // Undef enum discriminant. In an arry to avoid `Scalar` layout.
-const BAD_ENUM3 : [Enum2; 2] = [unsafe { TransmuteEnum2 { c: () }.b }; 2];
+const BAD_ENUM_UNDEF: [Enum2; 2] = [unsafe { TransmuteEnum2 { in3: () }.out1 }; 2];
 //~^ ERROR is undefined behavior
 
-// Invalid enum field content (mostly to test printing of apths for enum tuple
+// Invalid enum field content (mostly to test printing of paths for enum tuple
 // variants and tuples).
 union TransmuteChar {
     a: u32,
     b: char,
 }
 // Need to create something which does not clash with enum layout optimizations.
-const BAD_ENUM_CHAR : Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
+const BAD_ENUM_CHAR: Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
 //~^ ERROR is undefined behavior
 
 fn main() {
index 57c41711536d9485749b501a0f5cf85d87258306..7f1560558235f64d702a45bf3c5eaa4da36cb476 100644 (file)
@@ -1,35 +1,51 @@
 error[E0080]: it is undefined behavior to use this value
   --> $DIR/ub-enum.rs:24:1
    |
-LL | const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.b };
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected a valid enum discriminant
+LL | const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.out };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected a valid enum discriminant
    |
    = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
 
 error[E0080]: it is undefined behavior to use this value
-  --> $DIR/ub-enum.rs:38:1
+  --> $DIR/ub-enum.rs:43:1
    |
-LL | const BAD_ENUM2 : Enum2 = unsafe { TransmuteEnum2 { a: 0 }.b };
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected a valid enum discriminant
+LL | const BAD_ENUM2: Enum2 = unsafe { TransmuteEnum2 { in1: 0 }.out1 };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected a valid enum discriminant
    |
    = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
 
 error[E0080]: it is undefined behavior to use this value
-  --> $DIR/ub-enum.rs:42:1
+  --> $DIR/ub-enum.rs:45:1
    |
-LL | const BAD_ENUM3 : [Enum2; 2] = [unsafe { TransmuteEnum2 { c: () }.b }; 2];
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
+LL | const BAD_ENUM3: Enum2 = unsafe { TransmuteEnum2 { in2: &0 }.out1 };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected a valid enum discriminant
    |
    = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
 
 error[E0080]: it is undefined behavior to use this value
-  --> $DIR/ub-enum.rs:52:1
+  --> $DIR/ub-enum.rs:47:1
    |
-LL | const BAD_ENUM_CHAR : Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 4294967295 at .Some.0.1, but expected something in the range 0..=1114111
+LL | const BAD_ENUM4: Wrap<Enum2> = unsafe { TransmuteEnum2 { in2: &0 }.out2 };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected something that cannot possibly fail to be in the range 2..=2
    |
    = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
 
-error: aborting due to 4 previous errors
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/ub-enum.rs:51:1
+   |
+LL | const BAD_ENUM_UNDEF: [Enum2; 2] = [unsafe { TransmuteEnum2 { in3: () }.out1 }; 2];
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
+   |
+   = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
+
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/ub-enum.rs:61:1
+   |
+LL | const BAD_ENUM_CHAR: Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 4294967295 at .Some.0.1, but expected something in the range 0..=1114111
+   |
+   = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
+
+error: aborting due to 6 previous errors
 
 For more information about this error, try `rustc --explain E0080`.