From ff241115c44dcdaeb1e893abae14fc702298f6a7 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Mon, 27 Sep 2021 21:51:53 -0600 Subject: [PATCH] forbid bit-selects and part-selects of scalar struct fields --- CHANGELOG.md | 2 ++ src/Convert/Struct.hs | 9 +++++++++ test/error/struct_logic_bit.sv | 7 +++++++ test/error/struct_logic_part_range.sv | 7 +++++++ test/error/struct_logic_part_select.sv | 7 +++++++ 5 files changed, 32 insertions(+) create mode 100644 test/error/struct_logic_bit.sv create mode 100644 test/error/struct_logic_part_range.sv create mode 100644 test/error/struct_logic_part_select.sv diff --git a/CHANGELOG.md b/CHANGELOG.md index a63d9d5a..b0f8268a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ * Non-positive integer size casts are now detected and forbidden * Negative indices in struct pattern literals are now detected and forbidden * Escaped vendor block comments in macro bodies are now tolerated +* Illegal bit-selects and part-selects of scalar struct fields are now detected + and forbidden, rather than yielding an internal assertion failure ### Bug Fixes diff --git a/src/Convert/Struct.hs b/src/Convert/Struct.hs index d680f8b7..560be7ba 100644 --- a/src/Convert/Struct.hs +++ b/src/Convert/Struct.hs @@ -386,6 +386,9 @@ convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) = (UnknownType, orig') else if structIsntReady subExprType then (replaceInnerTypeRange NonIndexed rOuter' fieldType, orig') + else if null dims then + error $ "illegal access to range " ++ show (Range Nil NonIndexed rOuter) + ++ " of " ++ show (Dot e x) ++ ", which has type " ++ show fieldType else (replaceInnerTypeRange NonIndexed rOuter' fieldType, undotted) where @@ -408,6 +411,9 @@ convertSubExpr scopes (Range (Dot e x) mode (baseO, lenO)) = (UnknownType, orig') else if structIsntReady subExprType then (replaceInnerTypeRange mode (baseO', lenO') fieldType, orig') + else if null dims then + error $ "illegal access to range " ++ show (Range Nil mode (baseO, lenO)) + ++ " of " ++ show (Dot e x) ++ ", which has type " ++ show fieldType else (replaceInnerTypeRange mode (baseO', lenO') fieldType, undotted) where @@ -438,6 +444,9 @@ convertSubExpr scopes (Bit (Dot e x) i) = (dropInnerTypeRange backupType, orig') else if structIsntReady subExprType then (dropInnerTypeRange fieldType, orig') + else if null dims then + error $ "illegal access to bit " ++ show i ++ " of " ++ show (Dot e x) + ++ ", which has type " ++ show fieldType else (dropInnerTypeRange fieldType, Bit e' iFlat) where diff --git a/test/error/struct_logic_bit.sv b/test/error/struct_logic_bit.sv new file mode 100644 index 00000000..4400fd16 --- /dev/null +++ b/test/error/struct_logic_bit.sv @@ -0,0 +1,7 @@ +// pattern: illegal access to bit 0 of s\.x, which has type logic +module top; + struct packed { + logic x; + } s; + initial s.x[0] = 1; +endmodule diff --git a/test/error/struct_logic_part_range.sv b/test/error/struct_logic_part_range.sv new file mode 100644 index 00000000..1a6d2b36 --- /dev/null +++ b/test/error/struct_logic_part_range.sv @@ -0,0 +1,7 @@ +// pattern: illegal access to range \[0:0\] of s\.x, which has type logic +module top; + struct packed { + logic x; + } s; + initial s.x[0:0] = 1; +endmodule diff --git a/test/error/struct_logic_part_select.sv b/test/error/struct_logic_part_select.sv new file mode 100644 index 00000000..cd5a9853 --- /dev/null +++ b/test/error/struct_logic_part_select.sv @@ -0,0 +1,7 @@ +// pattern: illegal access to range \[0\+:1\] of s\.x, which has type logic +module top; + struct packed { + logic x; + } s; + initial s.x[0+:1] = 1; +endmodule