Literal definitions stranges

#1

Literal definition of signed literals have unary - for describe sign of number. As I think literal is udivide by parts for compiler, but I have example than broke this logic:

(-1i128).pow(-number as u32 + 1) * fib.value

Than I remove braces from -1i128 compile interpret this expression as:

-(1i128.pow(-number as u32 + 1)) * fib.value

I think it’s strange for perception.

#2

A simpler example is -1i8.abs() vs (-1i8).abs(). This difference is kind of annoying given that -1i8 is seen as a literal by the macro system:

macro_rules! yes_this_is_a_literal {
    ($val:literal) => ();
}
yes_this_is_a_literal!(-1i8);

Changing this would be a breaking change for Rust though…

1 Like
#3

I uderstand about breaking, but I think the simplifying syntax is better than less changes.

#4

Rust has strong guarantees about breaking changes, so we can’t make a breaking change for this (it is not unsound behavior).

2 Likes
#5

So, it may be added to rust 2019 edition, or another future proposals.

1 Like
#6

For this to happen in an edition, it would need a lint warning about the change in behaviour. So if you wanted to make progress towards it, consider writing a lint – which may be valuable regardless of whether a change happens if people consider -1i8.abs() confusing.

(I don’t know the rules for clippy vs rustc here.)

#7

I believe - should be part of the integer literal, not a tack-on token. Esp. in light of what @mjbshaw wrote - it IS a literal, so why the minus sign is treated separately?

Can somebody describe the necessary steps for writing such a lint in more detail so that we could implement it and add to rustc (or clippy? not sure) so it can be made part of some edition. I would love to see this changed in a further edition to a more sane semantics where -1u8.abs() returns 1 not -1.

#8

I’ve proposed to add a note about this to the book: https://github.com/rust-lang/book/issues/1930

1 Like
#9

clippy::precedence covers the problem with negative literals:
https://rust-lang.github.io/rust-clippy/master/index.html#precedence

7 Likes