From bf50178eb3988496d1f60b4b3f6e5c3a02103e6b Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 22 Jan 2026 20:04:09 +0100 Subject: [PATCH] $typeof() returns typeinfo, causing errors #2795 --- releasenotes.md | 1 + src/compiler/sema_expr.c | 1 - src/compiler/sema_types.c | 4 +++- test/test_suite/compile_time/typeof_ct.c3 | 6 ++++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/compile_time/typeof_ct.c3 diff --git a/releasenotes.md b/releasenotes.md index 6abff1ebe..abfec8851 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -106,6 +106,7 @@ - Too deeply nested scopes was a fatal crash and not a regular semantic error. #2796 - Recursive definition of tag not detected with nested tag/tagof #2790 - Attrdef eval environment lacked rtype, causing error on invalid args #2797 +- $typeof() returns typeinfo, causing errors #2795. ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index bd97830b7..bf72299d2 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -6900,7 +6900,6 @@ static inline bool sema_expr_analyse_cast(SemaContext *context, Expr *expr, bool { RETURN_SEMA_ERROR(type_info, "Casting to an optional type is not allowed."); } - if (invalid_cast_ref) { if (!cast_explicit_silent(context, inner, target_type)) diff --git a/src/compiler/sema_types.c b/src/compiler/sema_types.c index 97462e71c..0514a5418 100644 --- a/src/compiler/sema_types.c +++ b/src/compiler/sema_types.c @@ -341,10 +341,12 @@ INLINE bool sema_resolve_typeof(SemaContext *context, TypeInfo *type_info) { case STORAGE_ERROR: return false; + case STORAGE_COMPILE_TIME: + if (expr_type->type_kind == TYPE_TYPEINFO) expr_type = type_typeid; + FALLTHROUGH; case STORAGE_NORMAL: case STORAGE_VOID: case STORAGE_UNKNOWN: - case STORAGE_COMPILE_TIME: type_info->type = expr_type; return true; case STORAGE_WILDCARD: diff --git a/test/test_suite/compile_time/typeof_ct.c3 b/test/test_suite/compile_time/typeof_ct.c3 new file mode 100644 index 000000000..aeecd2ec4 --- /dev/null +++ b/test/test_suite/compile_time/typeof_ct.c3 @@ -0,0 +1,6 @@ +fn int main() +{ + int x; + double df2 = ($typeof(double))(x); // #error: You cannot cast 'int' to 'typeid' + return 0; +} \ No newline at end of file