diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 8ca845d2c..ee10cb925 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -4588,6 +4588,16 @@ ZigFn *scope_fn_entry(Scope *scope) { return nullptr; } +bool scope_is_comptime(Scope *scope) { + while (scope) { + if (scope->id == ScopeIdCompTime) { + return true; + } + scope = scope->parent; + } + return false; +} + ZigPackage *scope_package(Scope *scope) { ZigType *import = get_scope_import(scope); assert(is_top_level_struct(import)); diff --git a/src/stage1/analyze.hpp b/src/stage1/analyze.hpp index 2815274f6..2d9c12223 100644 --- a/src/stage1/analyze.hpp +++ b/src/stage1/analyze.hpp @@ -102,6 +102,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag); bool is_ref(ZigType *type_entry); bool is_array_ref(ZigType *type_entry); bool is_container_ref(ZigType *type_entry); +bool scope_is_comptime(Scope *scope); Error is_valid_vector_elem_type(CodeGen *g, ZigType *elem_type, bool *result); void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node); ZigFn *scope_fn_entry(Scope *scope); diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 2f345a841..a78fe0618 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -9524,6 +9524,9 @@ static IrInstSrc *ir_gen_switch_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n static IrInstSrc *ir_gen_comptime(IrBuilderSrc *irb, Scope *parent_scope, AstNode *node, LVal lval) { assert(node->type == NodeTypeCompTime); + if (scope_is_comptime(parent_scope)) { + add_node_error(irb->codegen, node, buf_sprintf("redundant comptime keyword in already comptime scope")); + } Scope *child_scope = create_comptime_scope(irb->codegen, node, parent_scope); // purposefully pass null for result_loc and let EndExpr handle it return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval, nullptr);