Commit 07f64a2
Sema: error on ambiguous coercion of comptime float and ints
The following, from the documentation as of the time of writing, illustrates
the problem:
```zig
// Compile time coercion of float to int
test "implicit cast to comptime_int" {
var f: f32 = 54.0 / 5;
_ = f;
}
```
It is not clear how to unify the types of 54.0 and 5 to perform the
division. We can either
- cast 54.0 to comptime_int resulting in @as(comptime_int, 10), which is
casted to @as(f32, 10), or
- cast 5 to comptime_float resulting in @as(comptime_float, 10.8), which
is casted to @as(f32, 10.8)
Since the two resulting values are different, a compiler error is appropriate.
If we know that casting to either type will result in the same value we
don't need to error. For instance, 10.0 / 2 is okay, as is 10 / 2.0.
Fixes: #123641 parent 070282a commit 07f64a2
2 files changed
+20
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11202 | 11202 | | |
11203 | 11203 | | |
11204 | 11204 | | |
| 11205 | + | |
| 11206 | + | |
| 11207 | + | |
| 11208 | + | |
| 11209 | + | |
| 11210 | + | |
| 11211 | + | |
| 11212 | + | |
| 11213 | + | |
| 11214 | + | |
| 11215 | + | |
| 11216 | + | |
| 11217 | + | |
| 11218 | + | |
| 11219 | + | |
| 11220 | + | |
11205 | 11221 | | |
11206 | 11222 | | |
11207 | 11223 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
198 | | - | |
| 197 | + | |
| 198 | + | |
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
231 | | - | |
232 | | - | |
| 231 | + | |
| 232 | + | |
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| |||
0 commit comments