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

stage2: add error for returning pointer to local variable #11153

Closed
wants to merge 3 commits into from

Conversation

Vexu
Copy link
Member

@Vexu Vexu commented Mar 13, 2022

The check does not apply to inline functions and wont apply to functions called at comptime depending on #11152.
Most simple cases are caught but it is easy to bypass if needed.

Some cases might be too aggressive but they are easy to remove if deemed so.

Closes #2646

@andrewrk
Copy link
Member

andrewrk commented Mar 14, 2022

One thing we need to do here is make sure we can manage to make the language specification compatible with this compile error.

cc @SpexGuy

Also related: #3180
I have an idea to try to solve use-after-free of locals at runtime by doing escape analysis, secretly allocating escaped things on the heap with GPA, then freeing them on return. I have not yet done a proof-of-concept for this idea.

Consider this problem:

fn foo() *i32 {
    return undefined;
}

This is valid zig code. It's effectively the same thing as returning the address of a local. We'll need to get creative with the lang spec to make this a compile error.

@Jarred-Sumner
Copy link
Contributor

This is valid zig code

I do this currently when implementing a large interface for something function by function

Just so the code compiles even though the implementation is incomplete

@andrewrk
Copy link
Member

@Jarred-Sumner why not instead @panic("TODO")? Or even just unreachable?

@andrewrk
Copy link
Member

Closing since we currently have no workable plan to make this a legal compile error according to the lang spec. We can revisit this patch when dealing with #2646 which now has a link to this closed PR. The next step towards landing this patch is coming up with a lang spec proposal that allows this compile error. Please make such proposals as comments on #2646.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Compiler Error when returning pointer to stack variable
3 participants