Skip to content

Commit 6d2ac8c

Browse files
committed
Check array aggregate discrete range type
1 parent 41835e9 commit 6d2ac8c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

vhdl_lang/src/analysis/expression.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,17 @@ impl<'a> AnalyzeContext<'a> {
996996
&mut index_expr.item,
997997
diagnostics,
998998
) {
999-
Ok(Some(_)) => {
1000-
// @TODO check type matches index type
999+
Ok(Some(typ)) => {
1000+
if let Some(index_type) = index_type {
1001+
if !self.can_be_target_type(typ, index_type) {
1002+
diagnostics.push(Diagnostic::type_mismatch(
1003+
&index_expr.pos,
1004+
&typ.describe(),
1005+
index_type.into(),
1006+
));
1007+
}
1008+
}
1009+
10011010
can_be_array = true;
10021011
}
10031012
Ok(None) => {

vhdl_lang/src/analysis/tests/typecheck_expression.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,15 +949,25 @@ constant good2 : string(1 to 6) := (\"text\", others => ' ');
949949
#[test]
950950
fn array_element_association_may_be_type_denoting_discrete_range() {
951951
let mut builder = LibraryBuilder::new();
952-
builder.in_declarative_region(
952+
let code = builder.in_declarative_region(
953953
"
954954
subtype sub_t is natural range 1 to 3;
955-
constant good1 : integer_vector := (sub_t => 0);
955+
constant good : integer_vector := (sub_t => 0);
956+
957+
subtype csub_t is character range 'a' to 'b';
958+
constant bad : integer_vector := (csub_t => 0);
959+
956960
",
957961
);
958962

959963
let diagnostics = builder.analyze();
960-
check_no_diagnostics(&diagnostics);
964+
check_diagnostics(
965+
diagnostics,
966+
vec![Diagnostic::error(
967+
code.s1("csub_t =>").s1("csub_t"),
968+
"subtype 'csub_t' does not match integer type 'INTEGER'",
969+
)],
970+
);
961971
}
962972

963973
#[test]

0 commit comments

Comments
 (0)