Skip to content

Commit c2199f6

Browse files
committed
Fix for inferring types when promoting constants
1 parent 46a5347 commit c2199f6

3 files changed

Lines changed: 590 additions & 1 deletion

File tree

examples/03_mixed_types.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ int main() {
2828
};
2929

3030
// Compile with ME_AUTO to infer output type from inputs
31+
// NOTE: With ME_AUTO, any constants in the expression would
32+
// be inferred from the first variable's type (ME_INT32 here)
33+
// But this expression has no constants, only variables
3134
int error;
3235
me_expr *expr = me_compile("a + b", vars, 2, ME_AUTO, &error);
3336

src/miniexpr.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,16 @@ static me_expr *private_compile(const char *expression, const me_variable *varia
30383038
s.start = s.next = expression;
30393039
s.lookup = vars_copy ? vars_copy : variables;
30403040
s.lookup_len = var_count;
3041-
s.target_dtype = (dtype != ME_AUTO) ? dtype : ME_FLOAT64; // Set target dtype for constants
3041+
// When dtype is ME_AUTO, infer target dtype from variables to avoid type mismatch
3042+
if (dtype != ME_AUTO) {
3043+
s.target_dtype = dtype;
3044+
} else if (variables && var_count > 0) {
3045+
// Use the first variable's dtype as the target for constants
3046+
// This prevents type promotion issues when mixing float32 vars with float64 constants
3047+
s.target_dtype = variables[0].dtype;
3048+
} else {
3049+
s.target_dtype = ME_FLOAT64; // Fallback to double
3050+
}
30423051

30433052
next_token(&s);
30443053
me_expr *root = list(&s);

0 commit comments

Comments
 (0)