@@ -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]
51785197fn 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}
0 commit comments