Idea: In the next edition, stop accepting `0.` as a valid float literal

FWIW, I am also a fan of expanding type inference on literals. A long time ago I proposed making let x: String = "abc"; compile, and I think making let x: f32 = 0; compile is also a good idea for the same basic reason: It's extremely obvious what the type has to be, so forcing the user to go through a compile error -> add to_string() / .0 -> rebuild cycle is just being pedantic in a way that "makes simple things hard" and primarily affects the sort of toy programs that impact a first-time user's early impressions of Rust. I would also expect let x: f32 = 0; to be less controversial because it doesn't involve type inference changing a stack variable into a heap allocation.

A lot of that old thread got sidetracked by much broader and/or more magical proposals that never really had a chance, but AFAIK the only serious problem with these ideas back then was:

and my impression is that even today, this is still not a fully solved problem in chalk/rustc.


To move the discussion forward: assume for now that this is solvable, and we live in the happy timeline where someday it will be implementable in rustc with "no risk". Would anyone object to making let x: f32 = 0; compile, i.e. allowing numeric literals to infer to floating point types as well as integer types?

If everyone is neutral or in favor of that (as I currently suspect we are), then I think it makes perfect sense to postpone any changes to 0. until after we've made let x: f32 = 0; compile.

14 Likes