Skip to content

Getting @typeInfo of a struct inside that struct causes Zig to segfault #2968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
heidezomp opened this issue Jul 29, 2019 · 4 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@heidezomp
Copy link
Contributor

I was exploring the boundaries of the compile-time reflection capabilities of Zig (which are very cool!!) when I found that calling @typeInfo on a struct inside that same struct causes the compiler to segfault. Doing this probably isn't useful; I can't think of what the result should be (is this is even theoretically possible?). So this should probably result in a compile-time error?

Example code:

const foo = struct {
    const info = @typeInfo(foo);
};

pub fn main() void {
    const info = foo.info;
}
% zig build-exe typeinfo_segfault.zig
Segmentation fault at address 0x18
???:?:?: 0x557480bade39 in ??? (???)


zsh: segmentation fault  zig build-exe typeinfo_segfault.zig

Backtrace in gdb:

(gdb) run
Starting program: /home/heidezomp/zig/build/bin/zig build-exe typeinfo_segfault.zig
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000555555a61e39 in ir_make_type_info_decls(IrAnalyze*, IrInstruction*, ConstExprValue*, ScopeDecls*) ()
(gdb) bt
#0  0x0000555555a61e39 in ir_make_type_info_decls(IrAnalyze*, IrInstruction*, ConstExprValue*, ScopeDecls*) ()
#1  0x0000555555a62dd0 in ir_make_type_info_value(IrAnalyze*, IrInstruction*, ZigType*, ConstExprValue**) ()
#2  0x0000555555aa32d9 in ir_analyze_instruction_base(IrAnalyze*, IrInstruction*) ()
#3  0x0000555555aa418b in ir_analyze(CodeGen*, IrExecutable*, IrExecutable*, ZigType*, AstNode*) ()
#4  0x0000555555aa448e in ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigType*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutable*, AstNode*) ()
#5  0x0000555555ad3c7f in analyze_const_value(CodeGen*, Scope*, AstNode*, ZigType*, Buf*) ()
#6  0x0000555555ae56e4 in resolve_decl_var(CodeGen*, TldVar*) ()
#7  0x0000555555ae7a2b in resolve_top_level_decl(CodeGen*, Tld*, AstNode*) [clone .part.76] ()
#8  0x0000555555a8b724 in ir_analyze_decl_ref(IrAnalyze*, IrInstruction*, Tld*) ()
#9  0x0000555555a9ee10 in ir_analyze_instruction_field_ptr(IrAnalyze*, IrInstructionFieldPtr*) ()
#10 0x0000555555aa2c93 in ir_analyze_instruction_base(IrAnalyze*, IrInstruction*) ()
#11 0x0000555555aa418b in ir_analyze(CodeGen*, IrExecutable*, IrExecutable*, ZigType*, AstNode*) ()
#12 0x0000555555ae0df9 in analyze_fn_ir(CodeGen*, ZigFn*, AstNode*) ()
#13 0x0000555555ae0b1f in analyze_fn_body(CodeGen*, ZigFn*) [clone .part.77] ()
#14 0x0000555555ae7ea3 in semantic_analyze(CodeGen*) ()
#15 0x0000555555a4df68 in codegen_build_and_link(CodeGen*) ()
#16 0x0000555555a1c0c5 in main ()

This is using a release build of Zig from the master branch.

@emekoi
Copy link
Contributor

emekoi commented Jul 29, 2019

I think this will be solved by #2174.

@heidezomp
Copy link
Contributor Author

@emekoi Thanks for the pointer, that looks like an interesting issue! I will try to digest it as best I can, and hopefully learn more about how Zig works internally along the way 😄

@andrewrk andrewrk added this to the 0.5.0 milestone Jul 29, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jul 29, 2019
@heidezomp
Copy link
Contributor Author

This was not solved by #2174.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 11, 2019
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Feb 10, 2020
@andrewrk
Copy link
Member

This now gives:

./test2.zig:2:18: error: dependency loop detected
    const info = @typeInfo(foo);
                 ^
./test2.zig:6:21: note: referenced here
    const info = foo.info;
                    ^

Related: #3893

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants