diag: explain pointer-sized cast restrictions in source advice

This commit is contained in:
dev committed 2026-10-09 21:01:58 +03:00
1 parent 8a714ba6de
commit 6a02659f31
23 files changed
+96 -8

No files matched your search

@@ -0,0 +1,13 @@
// expect: run
// result: 84
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.explicit
fn same(x: isize) -> isize effects [] { return x as! isize; }
fn main() -> u64 effects [] {
let x: usize = 42u8 as! usize;
let y: u64 = x as! u64;
let z: isize = 42i8 as% isize;
let q: i64 = same(z) as! i64;
return y + (q as! u64);
}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: i8) -> isize effects [] { return x as isize; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: isize) -> i64 effects [] { return x as i64; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: isize) -> isize effects [] { return x as isize; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: u8) -> usize effects [] { return x as usize; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: usize) -> u64 effects [] { return x as u64; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/02-types-and-values.md §2; docs/spec/15-source-language.md §5
module cast.rejection
fn f(x: usize) -> usize effects [] { return x as usize; }
fn main() -> void effects [] {}
@@ -0,0 +1 @@
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -1,2 +1,2 @@
[{"kind":"numeric_cast_modes","hint":"Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
[{"kind":"numeric_cast_modes","hint":"Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
@@ -1 +1 @@
[{"kind":"numeric_cast_modes","hint":"Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
[{"kind":"numeric_cast_modes","hint":"The target-independent `as` rule excludes `usize`/`isize`, even when the value fits. Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.","alternatives":["as!","as%"]}]
+25
View File
@@ -157,4 +157,29 @@ with tempfile.TemporaryDirectory(prefix='source-advice-', dir=cache) as director
probe.write_text('module probe\n' + body + '\n')
advice(json.loads(cli('check', probe, code=1)), kind)
# Pointer-sized widening is rejected independently of target/value range.
# Identity casts follow the same source rule instead of leaking AIR E1002.
for source_type, target_type in [
('u8', 'usize'), ('usize', 'u64'), ('i8', 'isize'),
('isize', 'i64'), ('usize', 'usize'), ('isize', 'isize'),
]:
base = (f'module probe\nfn f(x: {source_type}) -> {target_type} effects [] '
'{ return x OP ' + target_type + '; }\n'
'fn main() -> void effects [] {}\n')
probe.write_text(base.replace('OP', 'as'))
error = json.loads(cli('check', probe, code=1))
advice(error, 'numeric_cast_modes', ['as!', 'as%'])
assert (error['from'], error['to']) == (source_type, target_type), error
assert 'air_line' not in error and 'air_col' not in error, error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, probe, code=1)) == error
for spelling in ('as!', 'as%'):
probe.write_text(base.replace('OP', spelling))
cli('check', probe)
for spelling in ('u8', 'u64', 'i64'):
probe.write_text(f'module probe\nfn f(x: {spelling}) -> {spelling} effects [] '
f'{{ return x as {spelling}; }}\n'
'fn main() -> void effects [] {}\n')
cli('check', probe)
print(f'source repair advice: {checks} checks passed')