Skip to content
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

Assertion failed at ir.cpp:22760 in ir_analyze_container_member_access_inner #7813

Open
spadix0 opened this issue Jan 18, 2021 · 12 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@spadix0
Copy link

spadix0 commented Jan 18, 2021

While trying to factor some comptime arguments into a struct for cZPeg, I ran into this:

$ zig build test                               
All 25 tests passed.
Semantic Analysis [85/655] Assertion failed at /Users/runner/work/1/s/src/stage1/ir.cpp:22760 in ir_analyze_container_member_access_inner. This is a bug in the Zig compiler.The following command terminated unexpectedly:
/Users/spadix/zig-macos-x86_64-0.8.0-dev.1001+811833658/zig test /Users/spadix/proj/czpeg/src/czpeg/re.zig --cache-dir /Users/spadix/proj/czpeg/zig-cache --global-cache-dir /Users/spadix/.cache/zig --name test 
error: the following build command failed with exit code 1:
/Users/spadix/proj/czpeg/zig-cache/o/fcb79c9b92ca39c2295f175ae6ea2441/build /Users/spadix/zig-macos-x86_64-0.8.0-dev.1001+811833658/zig /Users/spadix/proj/czpeg /Users/spadix/proj/czpeg/zig-cache /Users/spadix/.cache/zig test

The code to reproduce is here: https://github.com/spadix0/cZPeg/tree/zig_assert_ir_analyze_container
that may not compile even without the assertion, but all tests pass before the branch, so something in that (small) diff probably triggered the problem(?)

stack trace
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00007fff736a133a libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00007fff7375de60 libsystem_pthread.dylib`pthread_kill + 430
    frame #2: 0x00007fff73628808 libsystem_c.dylib`abort + 120
    frame #3: 0x0000000100003866 zig`std.os.abort + 6
    frame #4: 0x00000001000048fe zig`std.debug.panicExtra + 910
    frame #5: 0x00000001000010d9 zig`std.builtin.default_panic + 25
    frame #6: 0x000000010000a32a zig`stage2_panic + 26
    frame #7: 0x00000001003684fc zig`zig_panic(char const*, ...) + 172
    frame #8: 0x00000001003f9c9c zig`ir_analyze_container_member_access_inner(IrAnalyze*, ZigType*, Buf*, IrInst*, IrInstGen*, IrInst*, ZigType*) + 828
    frame #9: 0x00000001003b5be3 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 66451
    frame #10: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #11: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #12: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #13: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #14: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #15: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #16: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #17: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #18: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #19: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #20: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #21: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #22: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #23: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #24: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #25: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #26: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #27: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #28: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #29: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #30: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #31: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #32: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #33: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #34: 0x00000001003aed75 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 38181
    frame #35: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #36: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #37: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #38: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #39: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #40: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #41: 0x00000001003a3201 zig`ir_eval_const_value(CodeGen*, Scope*, AstNode*, ZigValue*, unsigned long*, unsigned long*, ZigFn*, Buf*, AstNode*, Buf*, IrExecutableGen*, AstNode*, UndefAllowed) + 593
    frame #42: 0x00000001004007b5 zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 5173
    frame #43: 0x00000001004005cc zig`ir_analyze_fn_call(IrAnalyze*, IrInst*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier, IrInstGen*, IrInst*, bool, IrInstGen**, unsigned long, IrInstGen*, ResultLoc*) + 4684
    frame #44: 0x00000001003ff341 zig`ir_analyze_fn_call_src(IrAnalyze*, IrInstSrcCall*, ZigFn*, ZigType*, IrInstGen*, IrInstGen*, IrInst*, CallModifier) + 401
    frame #45: 0x00000001003b1819 zig`ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) + 49097
    frame #46: 0x00000001003a4a5b zig`ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) + 1227
    frame #47: 0x000000010037d905 zig`analyze_fn_body(CodeGen*, ZigFn*) + 1269
    frame #48: 0x000000010037ef07 zig`semantic_analyze(CodeGen*) + 231
    frame #49: 0x000000010033c62f zig`codegen_build_object(CodeGen*) + 1439
    frame #50: 0x000000010033515e zig`zig_stage1_build_object + 2222
    frame #51: 0x00000001000dc8b0 zig`Compilation.performAllTheWork + 82544
    frame #52: 0x00000001000c1410 zig`Compilation.update + 1776
    frame #53: 0x0000000100089f4c zig`main.updateModule + 28
    frame #54: 0x000000010002cb2f zig`main.buildOutputType + 90319
    frame #55: 0x0000000100004a87 zig`main + 391
    frame #56: 0x00007fff73559cc9 libdyld.dylib`start + 1
@mikdusan
Copy link
Member

I am unable to reproduce.

image

@spadix0
Copy link
Author

spadix0 commented Jan 18, 2021

@mikdusan

I am unable to reproduce.

Are you on the branch? I have this:

$ git checkout zig_assert_ir_analyze_container
$ git describe --long --all
heads/zig_assert_ir_analyze_container-0-g79708ba

@LemonBoy
Copy link
Contributor

The problem is at czpeg.zig:285:47, you're calling .parse on a anytype field.
anytype is represented in the stage1 compiler as a special opaque{} value for which get_container_scope returns null, that generates the error you're seeing.

@mikdusan
Copy link
Member

that was it -- wrong branch -- can reproduce now

fyi, it's tedious but you can isolate tests by filtering. zig build supports it.. but right now it's just easier to test manually:

zig test src/czpeg/re.zig
// crash

zig test src/czpeg/re.zig --test-filter "spc"
All 1 tests passed.

zig test src/czpeg/re.zig --test-filter "name"
All 1 tests passed.

...

zig test src/czpeg/re.zig --test-filter "str"
// crash

This can be isolated further.

@spadix0
Copy link
Author

spadix0 commented Jan 18, 2021

@LemonBoy Thanks for helping me find where this error comes from! It is a bit strange to me that just moving the anytype into a struct causes my problem – that same anytype directly as a function parameter (before the branch) seems to be working... or it's possible I failed at testing. Also, the compiler must know somehow that it's a type in order to get down the @typeInfo switch?

@mikdusan Thanks for the testing tip! I will try try it out

@spadix0
Copy link
Author

spadix0 commented Jan 18, 2021

@LemonBoy Hey you're right – just casting it fixes that assertion and tests passing again!

diff --git a/src/czpeg.zig b/src/czpeg.zig
index c3246c9..24f85d8 100644
--- a/src/czpeg.zig
+++ b/src/czpeg.zig
@@ -282,7 +282,7 @@ pub const Pattern = struct {
                         break :blk if (comptime canError(@TypeOf(aerr))) try aerr else aerr;
                     },
                     .Type => blk: {
-                        var aerr = folder.init.parse(p);
+                        var aerr = @as(type, folder.init).parse(p);
                         var aopt = if (comptime canError(@TypeOf(aerr))) try aerr else aerr;
                         if (aopt) |a| {
                             break :blk a;

@mikdusan
Copy link
Member

@LemonBoy I'll put together a PR for this if you're not doing it already?

@LemonBoy
Copy link
Contributor

LemonBoy commented Jan 18, 2021

@mikdusan sure thing I can't read, please go ahead and cc me on the PR

mikdusan added a commit to mikdusan/zig that referenced this issue Jan 18, 2021
- prevents stage1 panic
- error "no field named '...' in '(anytype)' (opaque has no fields)"
- add test case

closes ziglang#7813
@mikdusan
Copy link
Member

@LemonBoy see #7815

@mikdusan mikdusan added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Jan 18, 2021
@mikdusan
Copy link
Member

with #7815 applied,

image

@spadix0
Copy link
Author

spadix0 commented Jan 18, 2021

Yea, that's great! I could probably debug from that; and esp like "opaque has no fields" extra hint. (although, maybe "opaque type ..." could be clearer if the reader wasn't familiar where it's coming from?)

I built this PR and can confirm that it also fixes the assertion for me.

Is there any plan to make this work without the cast in the future? I'm sure there is subtle context here that I don't have, but to me "compile-time duck typing" means that if the compiler knows the thing has a method (which it must, somewhere, because I can test for and cast it), then we should be able to call that method without extra ceremony?

Thanks for your help! yinz rock 🤘

@mikdusan
Copy link
Member

mikdusan commented Jan 18, 2021

Is there any plan to make this work without the cast in the future?

I believe so. LemonBoy has a better idea than just this error fix and might give that a go if it's not too complex.

mikdusan added a commit to mikdusan/zig that referenced this issue Jan 20, 2021
- prevents stage1 panic
- error "no field named '...' in '(anytype)' (opaque has no fields)"
- add test case

closes ziglang#7813
mikdusan added a commit to mikdusan/zig that referenced this issue Jan 24, 2021
- prevents stage1 panic
- error "no field named '...' in '(anytype)' (opaque has no fields)"
- add test case

closes ziglang#7813
@Vexu Vexu added this to the 0.9.0 milestone Jan 26, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
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

Successfully merging a pull request may close this issue.

5 participants