Skip to content

Commit 53ca1ef

Browse files
authored
fix(ir): compile unsupported float widths with OTP warnings
Unsupported literal float widths produce warnings instead of rejecting the module. Construction retains runtime errors, and impossible patterns still fail to match. A dedicated warning code keeps editor suppression separate from binary-segment errors.
1 parent a9ab107 commit 53ca1ef

8 files changed

Lines changed: 153 additions & 43 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Unsupported float segment widths compile as on OTP 29
6+
7+
- Literal float segment widths outside the supported encodings no longer reject
8+
the entire module, including widths scaled by a unit. They emit OTP's
9+
`bad float bit size` warning instead of a compile error.
10+
- Construction retains runtime `badarg` failures, while impossible patterns
11+
fall through or raise `badmatch` in a body match. Valid 16-, 32- and 64-bit
12+
encodings remain unchanged.
13+
- `binary_float_unsupported_width` runs unchanged natively and on OTP 29,
14+
covering evaluation order, guards, handlers, heads, funs, receive and
15+
list/binary comprehensions.
16+
517
## Nested case bindings remain safe outer inputs
618

719
- A variable bound on every path through nested cases remains available after

‎crates/nera-ir/src/ir.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub enum Instruction {
226226
/// result = binary_get_float(bin, bit_offset, bit_size, flags)
227227
/// Extracts a float value (IEEE 754) from the binary at the given BIT offset.
228228
/// Returns a boxed finite float, or nil on match failure.
229-
/// bit_offset is in bits; bit_size must be 16, 32 or 64.
229+
/// bit_offset is in bits; bit_size values other than 16, 32 or 64 return nil.
230230
BinaryGetFloat(VarId, VarId, VarId, VarId, BinFlags),
231231

232232
/// result = binary_get_binary(bin, bit_offset, bit_count)

‎crates/nera-ir/src/warnings/binary_segments.rs‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Validates binary segment type specifiers.
22
//!
33
//! Detects invalid combinations that OTP's compiler rejects at compile time:
4-
//! - Float size must be 16, 32, or 64 bits
54
//! - UTF types cannot have explicit size
65
//! - Explicit fields must not conflict; irrelevant flags are accepted as in OTP
76
//! - Unit does not apply to UTF types
87
//! - A string literal in a pattern takes no size and only a UTF type
8+
//!
9+
//! Unsupported literal float widths warn, but do not prevent compilation.
910
1011
use nera_syntax::ast::{self, BinSegmentKind, BinSpecifier};
1112
use nera_syntax::span::Span;
@@ -489,21 +490,15 @@ fn validate_segment(
489490
// OTP accepts signedness and endianness even when ignored.
490491
}
491492
BinSegmentKind::Float => {
492-
// A literal size must give 16, 32 or 64 bits once scaled by the
493-
// unit, as in OTP's bit_size_check: `<<X:1/float-unit:64>>` is valid.
493+
// OTP warns without rejecting the module. Construction validates
494+
// widths at runtime, while impossible patterns fall through.
494495
if let Some(size_expr) = size {
495496
if let ast::Expr::Integer(n, _) = size_expr.as_ref() {
496497
let bits = n.checked_mul(i64::from(type_spec.unit.unwrap_or(1)));
497498
if !matches!(bits, Some(16 | 32 | 64)) {
498499
diagnostics.push(
499-
Diagnostic::error_at(
500-
format!(
501-
"invalid size {} for float segment (must be 16, 32, or 64)",
502-
bits.map_or_else(|| n.to_string(), |bits| bits.to_string())
503-
),
504-
span,
505-
)
506-
.with_code("invalid_binary_segment"),
500+
Diagnostic::warning_at("bad float bit size", span)
501+
.with_code("bad_float_bit_size"),
507502
);
508503
}
509504
}

‎crates/nera-ir/src/warnings/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! - Unused functions (defined but neither exported nor called locally)
88
//! - Illegal guard expressions (non-guard-safe calls or constructs in when clauses)
99
//! - Duplicate function definitions (same name/arity defined more than once)
10-
//! - Invalid binary segment type specifiers (e.g., float with wrong size, signedness on binary)
10+
//! - Invalid binary segment type specifiers (e.g., conflicting types, explicit size on UTF)
1111
//! - Illegal map construction (`:=` in `#{}` instead of `=>`)
1212
//! - Module name vs filename mismatch
1313
//! - Missing behaviour callbacks (gen_server, gen_statem, supervisor, application, gen_event)

‎crates/nera-ir/src/warnings/tests.rs‎

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ fn binary_segment_valid_float_32() {
25762576
}
25772577

25782578
#[test]
2579-
fn binary_segment_invalid_float_size() {
2579+
fn binary_segment_unsupported_float_size_is_not_a_compile_error() {
25802580
let src = r#"
25812581
-module(test).
25822582
-export([f/1]).
@@ -2586,10 +2586,11 @@ fn binary_segment_invalid_float_size() {
25862586
let ws = analyze_module(&module, &names, None);
25872587
let msgs = warning_messages(&ws);
25882588
assert!(
2589-
msgs.iter().any(|m| m.contains("invalid size") && m.contains("float")),
2590-
"expected invalid float size error, got: {:?}",
2589+
!ws.iter().any(|d| d.severity == nera_syntax::error::Severity::Error),
2590+
"OTP compiles unsupported float widths for runtime validation: {:?}",
25912591
msgs
25922592
);
2593+
assert_eq!(msgs, vec!["bad float bit size"]);
25932594
}
25942595

25952596
#[test]
@@ -2680,7 +2681,7 @@ fn binary_segment_valid_integer_signed_little() {
26802681
}
26812682

26822683
#[test]
2683-
fn binary_segment_pattern_invalid_float_size() {
2684+
fn binary_segment_impossible_float_pattern_is_not_a_compile_error() {
26842685
let src = r#"
26852686
-module(test).
26862687
-export([f/1]).
@@ -2690,10 +2691,11 @@ fn binary_segment_pattern_invalid_float_size() {
26902691
let ws = analyze_module(&module, &names, None);
26912692
let msgs = warning_messages(&ws);
26922693
assert!(
2693-
msgs.iter().any(|m| m.contains("invalid size") && m.contains("float")),
2694-
"expected invalid float size error in pattern, got: {:?}",
2694+
!ws.iter().any(|d| d.severity == nera_syntax::error::Severity::Error),
2695+
"OTP compiles impossible float patterns: {:?}",
26952696
msgs
26962697
);
2698+
assert_eq!(msgs, vec!["bad float bit size"]);
26972699
}
26982700

26992701
#[test]
@@ -5083,7 +5085,6 @@ fn comprehension_patterns_receive_binary_segment_checks() {
50835085
("<<X/binary>>", "binary fields without size are not allowed"),
50845086
("<<X/bits>>", "binary fields without size are not allowed"),
50855087
("<<_:8,X/binary>>", "binary fields without size are not allowed"),
5086-
("<<X:8/float>>", "invalid size 8 for float segment"),
50875088
("<<X:8/utf8>>", "size not allowed for utf8 segments"),
50885089
("<<X:8/utf16>>", "size not allowed for utf16 segments"),
50895090
("<<X:8/utf32>>", "size not allowed for utf32 segments"),
@@ -5098,12 +5099,10 @@ fn comprehension_patterns_receive_binary_segment_checks() {
50985099
&& d.message.contains(message)), "{body}: {diagnostics:?}");
50995100
}
51005101
}
5101-
for (pattern, invalid) in [("<<X/binary>>", false), ("<<X:8/float>>", true)] {
5102-
let (module, names) = parse_module(&format!("-module(p). f(L) -> [X || {pattern} <- L]."));
5103-
let diagnostics = analyze_module(&module, &names, None);
5104-
assert_eq!(diagnostics.iter().any(|d|
5105-
d.code.as_deref() == Some("invalid_binary_segment")), invalid);
5106-
}
5102+
let (module, names) = parse_module("-module(p). f(L) -> [X || <<X/binary>> <- L].");
5103+
let diagnostics = analyze_module(&module, &names, None);
5104+
assert!(!diagnostics.iter().any(|d|
5105+
d.code.as_deref() == Some("invalid_binary_segment")));
51075106
}
51085107

51095108
#[test]
@@ -5133,7 +5132,6 @@ fn binary_segment_checks_walk_call_targets() {
51335132
for body in [
51345133
"(fun() -> [X || <<X/binary>> <= B] end)()",
51355134
"(fun() -> << X || <<X/bits>> <= B >> end)()",
5136-
"(fun() -> <<X:8/float>> = B end)()",
51375135
"(fun() -> <<_:8/utf8>> = B end)()",
51385136
] {
51395137
let (module, names) = parse_module(&format!("-module(p). f(B) -> {body}."));
@@ -5174,31 +5172,57 @@ fn binary_segment_checks_walk_guards_sizes_keys_and_record_defaults() {
51745172
}
51755173
}
51765174

5175+
#[test]
5176+
fn suppressing_float_width_warning_keeps_binary_segment_errors() {
5177+
// The float-width warning must be independently suppressible: opting out
5178+
// must not hide the UTF size error in the same function.
5179+
let src = r#"
5180+
-module(test).
5181+
-export([f/1]).
5182+
%% @nera:nowarn(bad_float_bit_size)
5183+
f(X) -> {<<X:24/float>>, <<X:8/utf8>>}.
5184+
"#;
5185+
let (module, names, source) = parse_with_source(src);
5186+
let diagnostics = analyze_module_with_source(&module, &names, None, Some(&source));
5187+
let remaining: Vec<_> = diagnostics.iter()
5188+
.map(|d| (d.severity, d.code.as_deref(), d.message.as_str())).collect();
5189+
assert_eq!(remaining, vec![(
5190+
nera_syntax::error::Severity::Error,
5191+
Some("invalid_binary_segment"),
5192+
"size not allowed for utf8 segments",
5193+
)]);
5194+
}
5195+
51775196
#[test]
51785197
fn float_segment_size_is_scaled_by_unit() {
5179-
// NERA-103 F2: OTP's bit_size_check accepts a float field whose size
5180-
// times unit is 16, 32 or 64 bits, so `<<X:1/float-unit:64>>` is valid.
5181-
let errors = |body: &str| -> Vec<String> {
5182-
let (module, names) = parse_module(&format!("-module(p). f(B) -> {body}."));
5183-
analyze_module(&module, &names, None).into_iter()
5184-
.filter(|d| d.code.as_deref() == Some("invalid_binary_segment"))
5185-
.map(|d| d.message).collect()
5186-
};
5187-
for segment in ["X:1/float-unit:64", "X:2/float-unit:32", "X:4/float-unit:16", "X:8/float-unit:8"] {
5198+
// OTP 29 accepts supported and unsupported size/unit products at compile
5199+
// time. The native regression owns their runtime results.
5200+
use nera_syntax::error::Severity;
5201+
for (segment, unsupported) in [
5202+
("X:1/float-unit:64", false), ("X:2/float-unit:32", false),
5203+
("X:4/float-unit:16", false), ("X:8/float-unit:8", false),
5204+
("X:2/float-unit:64", true), ("X:3/float-unit:8", true),
5205+
("X:1/float", true), ("X:8/float", true),
5206+
] {
51885207
for body in [
51895208
format!("<<{segment}>>"),
51905209
format!("<<{segment}>> = B"),
51915210
format!("[X || <<{segment}>> <= B]"),
51925211
format!("<< <<X:64/float>> || <<{segment}>> <= B >>"),
51935212
format!("[X || <<{segment}>> <- B]"),
5213+
format!("(fun() -> <<{segment}>> = B end)()"),
51945214
] {
5195-
assert_eq!(errors(&body), Vec::<String>::new(), "{body}");
5215+
let (module, names) = parse_module(&format!("-module(p). f(X, B) -> {body}."));
5216+
let diagnostics = analyze_module(&module, &names, None);
5217+
assert!(!diagnostics.iter().any(|d| d.severity == Severity::Error),
5218+
"{body}: {diagnostics:?}");
5219+
let segment_diagnostics: Vec<_> = diagnostics.iter()
5220+
.filter(|d| d.code.as_deref() == Some("bad_float_bit_size"))
5221+
.map(|d| (d.severity, d.message.as_str())).collect();
5222+
let expected = if unsupported {
5223+
vec![(Severity::Warning, "bad float bit size")]
5224+
} else { vec![] };
5225+
assert_eq!(segment_diagnostics, expected, "{body}");
51965226
}
51975227
}
5198-
assert_eq!(errors("<<X:2/float-unit:64>> = B"),
5199-
vec!["invalid size 128 for float segment (must be 16, 32, or 64)"]);
5200-
assert_eq!(errors("[X || <<X:3/float-unit:8>> <= B]"),
5201-
vec!["invalid size 24 for float segment (must be 16, 32, or 64)"]);
5202-
assert_eq!(errors("<<X:1/float>> = B"),
5203-
vec!["invalid size 1 for float segment (must be 16, 32, or 64)"]);
52045228
}

‎docs/DEVELOPER_TOOLS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ that editors can match on.
6565
| Code | Message | Severity |
6666
|------|---------|----------|
6767
| `invalid_binary_segment` | `invalid binary segment: {details}` | Error |
68+
| `bad_float_bit_size` | `bad float bit size` | Warning |
6869
| `conflicting_binary_specifiers` | `conflict in {type/sign/endianness/unit} specification for bit field: '{new}' and '{previous}'` | Error |
6970

7071
### Maps

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
222222
| `binary_split_patterns.erl` | binary:split/2,3 with a list of patterns: one-element lists, Cowlib's server-sent events line and field splits, longest match at the same offset, non-overlapping global splits, trim_all, a 1,000-line stream and a view into it, apply and fun calls, badarg patterns; also run on BEAM | Table-driven |
223223
| `binary_longest_common_prefix.erl` | Complete proper-list/binary validation, shared/shifted prefixes and dynamic calls | 17 |
224224
| `binary_float.erl` | Float binary segments (<<X:64/float>>) | ~6 |
225+
| `binary_float_unsupported_width.erl` | Unsupported literal and unit-scaled float widths compile with warnings: runtime badarg, evaluation order, guard failure, impossible case/head/fun/body/receive patterns and list/binary generators; valid scaled encoding controls; same fixture on OTP 29 | Completion checked |
225226
| `binary_refc.erl` | Reference-counted large binaries | ~6 |
226227
| `binary_refc_advanced.erl` | Advanced refc binary operations | ~6 |
227228
| `multi_clause_binary_rest.erl` | Multi-clause binary prefix patterns | ~15 |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%% OTP 29 compiles unsupported float widths: construction raises at runtime,
2+
%% and patterns never match, even when the extracted value is ignored.
3+
%% EXPECT: binary_float_unsupported_width_ok
4+
-module(binary_float_unsupported_width).
5+
-export([test/0]).
6+
7+
test() ->
8+
lists:foreach(fun(F) -> badarg = outcome(F) end, [
9+
fun() -> <<1.0:0/float>> end,
10+
fun() -> <<1.0:3/float>> end,
11+
fun() -> <<1.0:8/float>> end,
12+
fun() -> <<1.0:24/float-little>> end,
13+
fun() -> <<1.0:48/float-native>> end,
14+
fun() -> <<1.0:128/float>> end,
15+
fun() -> <<1.0:3/float-unit:8>> end,
16+
fun() -> <<1.0:2/float-unit:64>> end,
17+
fun() -> <<"A":24/float>> end
18+
]),
19+
%% An unsupported size must not prevent evaluation of later segments.
20+
erase(trace),
21+
badarg = outcome(fun() -> <<(mark(value, 1.0)):24/float,
22+
(mark(later, 7)):8>> end),
23+
[later, value] = erase(trace),
24+
badarith = outcome(fun() -> <<1.0:24/float, (1 div id(0)):8>> end),
25+
erase(continued),
26+
caught = try
27+
<<1.0:24/float>>,
28+
put(continued, true)
29+
catch error:badarg -> caught
30+
end,
31+
undefined = erase(continued),
32+
fallback = if <<1.0:24/float>> =:= <<0:24>> -> matched;
33+
true -> fallback end,
34+
lists:foreach(fun(Bin) ->
35+
fallback = case Bin of <<_:3/float>> -> matched; _ -> fallback end,
36+
fallback = case Bin of <<_:8/float>> -> matched; _ -> fallback end,
37+
fallback = case Bin of <<_:24/float-little>> -> matched; _ -> fallback end,
38+
fallback = case Bin of <<_:48/float-native>> -> matched; _ -> fallback end,
39+
fallback = case Bin of <<_:128/float>> -> matched; _ -> fallback end,
40+
fallback = head(Bin),
41+
fallback = scaled_head(Bin),
42+
F = fun(<<X:24/float>>) -> X; (_) -> fallback end,
43+
fallback = F(Bin),
44+
{badmatch, Bin} = outcome(fun() -> <<_:24/float>> = Bin end)
45+
end, [<<>>, <<0:3>>, <<0:8>>, <<0:24>>, <<0:48>>, <<0:128>>]),
46+
[] = [X || <<X:24/float>> <- [<<0:24>>, <<0:48>>]],
47+
[] = [X || <<X:24/float>> <= <<0:48>>],
48+
[] = [matched || <<_:3/float-unit:8>> <= <<0:48>>],
49+
<<>> = << <<X:64/float>> || <<X:24/float>> <= <<0:48>> >>,
50+
self() ! <<0:24>>,
51+
fallback = receive <<_:24/float>> -> matched after 0 -> fallback end,
52+
receive <<0:24>> -> ok after 0 -> error(message_consumed) end,
53+
%% Supported sizes, including size times unit, keep their encoded values.
54+
<<16#3C, 0>> = <<1.0:1/float-unit:16>>,
55+
<<16#3F, 16#80, 0, 0>> = <<1.0:4/float-unit:8>>,
56+
<<16#3F, 16#F0, 0, 0, 0, 0, 0, 0>> = <<1.0:2/float-unit:32>>,
57+
{1.0, 1.0, 1.0} = valid(<<1.0:16/float, 1.0:32/float, 1.0:64/float>>),
58+
io:format("binary_float_unsupported_width_ok~n"),
59+
ok.
60+
61+
head(<<X:24/float>>) -> X;
62+
head(_) -> fallback.
63+
scaled_head(<<X:3/float-unit:8>>) -> X;
64+
scaled_head(<<X:2/float-unit:64>>) -> X;
65+
scaled_head(_) -> fallback.
66+
valid(<<H:1/float-unit:16, S:4/float-unit:8, D:2/float-unit:32>>) -> {H, S, D}.
67+
68+
outcome(F) ->
69+
try F() of
70+
Value -> {unexpected_return, Value}
71+
catch error:Reason -> Reason
72+
end.
73+
mark(Label, Value) ->
74+
Trace = case get(trace) of undefined -> []; Previous -> Previous end,
75+
put(trace, [Label | Trace]),
76+
Value.
77+
id(X) -> X.

0 commit comments

Comments
 (0)