@@ -1900,7 +1900,9 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
1900
1900
if (!enum_type->data .enumeration .reported_infinite_err ) {
1901
1901
enum_type->data .enumeration .is_invalid = true ;
1902
1902
enum_type->data .enumeration .reported_infinite_err = true ;
1903
- add_node_error (g, decl_node, buf_sprintf (" enum '%s' contains itself" , buf_ptr (&enum_type->name )));
1903
+ ErrorMsg *msg = add_node_error (g, decl_node,
1904
+ buf_sprintf (" enum '%s' contains itself" , buf_ptr (&enum_type->name )));
1905
+ emit_error_notes_for_ref_stack (g, msg);
1904
1906
}
1905
1907
return ErrorSemanticAnalyzeFail;
1906
1908
}
@@ -2071,8 +2073,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2071
2073
if (struct_type->data .structure .resolve_loop_flag ) {
2072
2074
if (struct_type->data .structure .resolve_status != ResolveStatusInvalid) {
2073
2075
struct_type->data .structure .resolve_status = ResolveStatusInvalid;
2074
- add_node_error (g, decl_node,
2076
+ ErrorMsg *msg = add_node_error (g, decl_node,
2075
2077
buf_sprintf (" struct '%s' contains itself" , buf_ptr (&struct_type->name )));
2078
+ emit_error_notes_for_ref_stack (g, msg);
2076
2079
}
2077
2080
return ErrorSemanticAnalyzeFail;
2078
2081
}
@@ -2296,7 +2299,9 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
2296
2299
if (!union_type->data .unionation .reported_infinite_err ) {
2297
2300
union_type->data .unionation .reported_infinite_err = true ;
2298
2301
union_type->data .unionation .is_invalid = true ;
2299
- add_node_error (g, decl_node, buf_sprintf (" union '%s' contains itself" , buf_ptr (&union_type->name )));
2302
+ ErrorMsg *msg = add_node_error (g, decl_node,
2303
+ buf_sprintf (" union '%s' contains itself" , buf_ptr (&union_type->name )));
2304
+ emit_error_notes_for_ref_stack (g, msg);
2300
2305
}
2301
2306
return ErrorSemanticAnalyzeFail;
2302
2307
}
@@ -2527,8 +2532,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2527
2532
return ErrorNone;
2528
2533
2529
2534
if (enum_type->data .enumeration .zero_bits_loop_flag ) {
2530
- add_node_error (g, enum_type->data .enumeration .decl_node ,
2535
+ ErrorMsg *msg = add_node_error (g, enum_type->data .enumeration .decl_node ,
2531
2536
buf_sprintf (" '%s' depends on itself" , buf_ptr (&enum_type->name )));
2537
+ emit_error_notes_for_ref_stack (g, msg);
2532
2538
enum_type->data .enumeration .is_invalid = true ;
2533
2539
return ErrorSemanticAnalyzeFail;
2534
2540
}
@@ -2800,8 +2806,9 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2800
2806
if (struct_type->data .structure .resolve_loop_flag ) {
2801
2807
if (struct_type->data .structure .resolve_status != ResolveStatusInvalid) {
2802
2808
struct_type->data .structure .resolve_status = ResolveStatusInvalid;
2803
- add_node_error (g, decl_node,
2809
+ ErrorMsg *msg = add_node_error (g, decl_node,
2804
2810
buf_sprintf (" struct '%s' contains itself" , buf_ptr (&struct_type->name )));
2811
+ emit_error_notes_for_ref_stack (g, msg);
2805
2812
}
2806
2813
return ErrorSemanticAnalyzeFail;
2807
2814
}
@@ -3239,7 +3246,7 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {
3239
3246
}
3240
3247
ScopeDecls *decls_scope = reinterpret_cast <ScopeDecls *>(scope);
3241
3248
buf_append_buf (buf, &decls_scope->container_type ->name );
3242
- buf_append_char (buf, NAMESPACE_SEP_CHAR);
3249
+ if ( buf_len (buf) != 0 ) buf_append_char (buf, NAMESPACE_SEP_CHAR);
3243
3250
buf_append_buf (buf, tld->name );
3244
3251
}
3245
3252
@@ -3775,8 +3782,16 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3775
3782
3776
3783
ZigType *explicit_type = nullptr ;
3777
3784
if (var_decl->type ) {
3778
- ZigType *proposed_type = analyze_type_expr (g, tld_var->base .parent_scope , var_decl->type );
3779
- explicit_type = validate_var_type (g, var_decl->type , proposed_type);
3785
+ if (tld_var->analyzing_type ) {
3786
+ ErrorMsg *msg = add_node_error (g, var_decl->type ,
3787
+ buf_sprintf (" type of '%s' depends on itself" , buf_ptr (tld_var->base .name )));
3788
+ emit_error_notes_for_ref_stack (g, msg);
3789
+ explicit_type = g->builtin_types .entry_invalid ;
3790
+ } else {
3791
+ tld_var->analyzing_type = true ;
3792
+ ZigType *proposed_type = analyze_type_expr (g, tld_var->base .parent_scope , var_decl->type );
3793
+ explicit_type = validate_var_type (g, var_decl->type , proposed_type);
3794
+ }
3780
3795
}
3781
3796
3782
3797
assert (!is_export || !is_extern);
@@ -3863,7 +3878,8 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3863
3878
if (tld->resolution != TldResolutionUnresolved)
3864
3879
return ;
3865
3880
3866
- tld->dep_loop_flag = true ;
3881
+ assert (tld->resolution != TldResolutionResolving);
3882
+ tld->resolution = TldResolutionResolving;
3867
3883
g->tld_ref_source_node_stack .append (source_node);
3868
3884
3869
3885
switch (tld->id ) {
@@ -3894,27 +3910,31 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3894
3910
}
3895
3911
3896
3912
tld->resolution = TldResolutionOk;
3897
- tld->dep_loop_flag = false ;
3898
3913
g->tld_ref_source_node_stack .pop ();
3899
3914
}
3900
3915
3916
+ Tld *find_container_decl (CodeGen *g, ScopeDecls *decls_scope, Buf *name) {
3917
+ // resolve all the use decls
3918
+ for (size_t i = 0 ; i < decls_scope->use_decls .length ; i += 1 ) {
3919
+ AstNode *use_decl_node = decls_scope->use_decls .at (i);
3920
+ if (use_decl_node->data .use .resolution == TldResolutionUnresolved) {
3921
+ preview_use_decl (g, use_decl_node);
3922
+ resolve_use_decl (g, use_decl_node);
3923
+ }
3924
+ }
3925
+
3926
+ auto entry = decls_scope->decl_table .maybe_get (name);
3927
+ return (entry == nullptr ) ? nullptr : entry->value ;
3928
+ }
3929
+
3901
3930
Tld *find_decl (CodeGen *g, Scope *scope, Buf *name) {
3902
3931
while (scope) {
3903
3932
if (scope->id == ScopeIdDecls) {
3904
3933
ScopeDecls *decls_scope = (ScopeDecls *)scope;
3905
3934
3906
- // resolve all the use decls
3907
- for (size_t i = 0 ; i < decls_scope->use_decls .length ; i += 1 ) {
3908
- AstNode *use_decl_node = decls_scope->use_decls .at (i);
3909
- if (use_decl_node->data .use .resolution == TldResolutionUnresolved) {
3910
- preview_use_decl (g, use_decl_node);
3911
- resolve_use_decl (g, use_decl_node);
3912
- }
3913
- }
3914
-
3915
- auto entry = decls_scope->decl_table .maybe_get (name);
3916
- if (entry)
3917
- return entry->value ;
3935
+ Tld *result = find_container_decl (g, decls_scope, name);
3936
+ if (result != nullptr )
3937
+ return result;
3918
3938
}
3919
3939
scope = scope->parent ;
3920
3940
}
@@ -4475,10 +4495,12 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
4475
4495
Buf resolved_root_src_dir = os_path_resolve (&pkg_root_src_dir, 1 );
4476
4496
Buf namespace_name = BUF_INIT;
4477
4497
buf_init_from_buf (&namespace_name, &package->pkg_path );
4478
- if (buf_len (&namespace_name) != 0 ) buf_append_char (&namespace_name, NAMESPACE_SEP_CHAR);
4479
- buf_append_mem (&namespace_name, buf_ptr (&noextname) + buf_len (&resolved_root_src_dir) + 1 ,
4480
- buf_len (&noextname) - (buf_len (&resolved_root_src_dir) + 1 ));
4481
- buf_replace (&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR);
4498
+ if (source_kind == SourceKindNonRoot) {
4499
+ if (buf_len (&namespace_name) != 0 ) buf_append_char (&namespace_name, NAMESPACE_SEP_CHAR);
4500
+ buf_append_mem (&namespace_name, buf_ptr (&noextname) + buf_len (&resolved_root_src_dir) + 1 ,
4501
+ buf_len (&noextname) - (buf_len (&resolved_root_src_dir) + 1 ));
4502
+ buf_replace (&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR);
4503
+ }
4482
4504
4483
4505
RootStruct *root_struct = allocate<RootStruct>(1 );
4484
4506
root_struct->package = package;
@@ -6806,3 +6828,16 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) {
6806
6828
}
6807
6829
return false ;
6808
6830
}
6831
+
6832
+ void emit_error_notes_for_ref_stack (CodeGen *g, ErrorMsg *msg) {
6833
+ size_t i = g->tld_ref_source_node_stack .length ;
6834
+ for (;;) {
6835
+ if (i == 0 )
6836
+ break ;
6837
+ i -= 1 ;
6838
+ AstNode *source_node = g->tld_ref_source_node_stack .at (i);
6839
+ if (source_node) {
6840
+ msg = add_error_note (g, msg, source_node, buf_sprintf (" referenced here" ));
6841
+ }
6842
+ }
6843
+ }
0 commit comments