Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #![allow(dead_code, unused_variables)]
  2. #![feature(const_generics)]
  3.  
  4.  
  5. // note: we are using an u8
  6. struct S<const N: u8>;
  7.  
  8.  
  9. // compiles and can be called S::<0>::foo();
  10. impl S<0> { fn foo() {} }
  11. // compiles and can be called S::<255>::foo();
  12. impl S<255> { fn foo() {} }
  13. // both of the above were expected to work ^^
  14.  
  15.  
  16.  
  17. // now, uncomment the following
  18. // impl S<256> { fn foo() {} }
  19.  
  20. // expected: overflowing literals error
  21.  
  22. // error[E0592]: duplicate definitions with name `foo`
  23. // --> src/lib.rs:6:13
  24. // |
  25. // 6 | impl S<0> { fn foo() {} }
  26. // | ^^^^^^^^^^^ duplicate definitions for `foo`
  27. // 7 |
  28. // 8 | impl S<256> { fn foo() {} }
  29. // | ----------- other definition for `foo`
  30.  
  31. // acts as if it overflowed to 0 and now conflicts
  32. // however, const X: u8 = 256; does warn about overflow.
  33.  
  34.  
  35.  
  36.  
  37. // uncomment the following
  38. //impl S<300000> { fn foo() {} }
  39.  
  40. // expected: the error that is returned
  41.  
  42. // error: literal out of range for `u8`
  43. // --> src/lib.rs:23:8
  44. // |
  45. // 23 | impl S<300000> { fn foo() {} }
  46. // | ^^^^^^
  47. // |
  48. // = note: #[deny(overflowing_literals)] on by default
  49.  
  50. // if we add, #![allow(overflowing_literals)], it compiles.
  51. // noting if overflow is undesired at const generic level.
  52.  
  53.  
  54.  
  55.  
  56. // uncomment the following
  57. // impl S<300000000> { fn foo() {} }
  58. // or
  59. // impl S<299999999> { fn foo() {} }
  60. // etc
  61.  
  62. // expected: overflowing literals
  63.  
  64. // error[E0592]: duplicate definitions with name `foo`
  65. // --> src/lib.rs:6:13
  66. // |
  67. // 6 | impl S<0> { fn foo() {} }
  68. // | ^^^^^^^^^^^ duplicate definitions for `foo`
  69. // ...
  70. // 10 | impl S<300000000> { fn foo() {} }
  71. // | ----------- other definition for `foo`
  72.  
  73. // 300000001 warns about overflow. the lint/error reports inconsistently.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement