Skip to content

Commit

Permalink
Handle ambigous argument to 'val attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigher committed Nov 13, 2023
1 parent 77a2b54 commit 0a2278f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
34 changes: 0 additions & 34 deletions vhdl_lang/src/analysis/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,40 +638,6 @@ impl<'a> AnalyzeContext<'a> {
}
}

/// An expression of any integer type
pub fn integer_expr(
&self,
scope: &Scope<'a>,
expr: &mut WithPos<Expression>,
diagnostics: &mut dyn DiagnosticHandler,
) -> FatalResult {
if let Some(types) = as_fatal(self.expr_type(scope, expr, diagnostics))? {
match types {
ExpressionType::Unambiguous(typ) => {
if !typ.base().is_any_integer() {
diagnostics.error(
&expr.pos,
format!("Expected integer type, got {}", typ.describe()),
)
}
}
ExpressionType::Ambiguous(types) => {
// @TODO does not check if type is ambiguous
if types.iter().any(|typ| !typ.is_any_integer()) {
diagnostics.error(&expr.pos, "Expected integer type")
}
}
ExpressionType::String | ExpressionType::Null | ExpressionType::Aggregate => {
diagnostics.error(
&expr.pos,
format!("Expected integer type, got {}", types.describe()),
)
}
}
}
Ok(())
}

/// An expression that is either boolean or implicitly boolean via ?? operator
pub fn boolean_expr(
&self,
Expand Down
36 changes: 34 additions & 2 deletions vhdl_lang/src/analysis/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,12 @@ impl<'a> AnalyzeContext<'a> {

if typ.base().is_discrete() {
if let Some(ref mut expr) = check_single_argument(name_pos, attr, diagnostics) {
self.integer_expr(scope, expr, diagnostics)?;
self.expr_with_ttyp(
scope,
self.universal_integer().into(),
expr,
diagnostics,
)?;
}
Ok(AttrResolveResult::Value(typ.base()))
} else {
Expand Down Expand Up @@ -2554,7 +2559,7 @@ constant c0 : arr_t := (others => 0);
diagnostics,
vec![Diagnostic::error(
code.s1("'a'"),
"Expected integer type, got type 'CHARACTER'",
"character literal does not match type universal_integer",
)],
);

Expand Down Expand Up @@ -2591,6 +2596,33 @@ constant c0 : arr_t := (others => 0);
);
}

/// This is a regression test
#[test]
fn val_attribute_with_overloaded_name() {
let test = TestSetup::new();

test.declarative_part(
"
impure function pop return integer is
begin
end function;
impure function pop return boolean is
begin
end function;
type enum_t is (alpha, beta);
",
);
let code = test.snippet("enum_t'val(pop)");
assert_eq!(
test.name_resolve(&code, None, &mut NoDiagnostics),
Ok(ResolvedName::Expression(DisambiguatedType::Unambiguous(
test.lookup_type("enum_t")
)))
);
}

#[test]
fn signal_attributes_on_non_signal() {
let test = TestSetup::new();
Expand Down

0 comments on commit 0a2278f

Please sign in to comment.