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

`nocapture` attribute is missing on function arguments #25759

Closed
dotdash opened this Issue May 24, 2015 · 6 comments

Comments

Projects
None yet
5 participants
@dotdash
Copy link
Contributor

dotdash commented May 24, 2015

In a function like fn foo(x: &i32), the pointer is known not to be captured by the function because its lifetime doesn't allow it. We used to emit the nocapture attribute for such function arguments but don't do so anymore.

@dotdash dotdash added the A-codegen label May 24, 2015

@dotdash

This comment has been minimized.

Copy link
Contributor Author

dotdash commented May 29, 2015

I briefly discussed this with @eddyb on irc the other day, and the way we used to set nocapture would allow for undefined behaviour in safe code:

fn foo(x: &i32) -> *const i32 {
    x as *const i32
}

Since we based the attribute purely on the fact that the pointer has an anonymous lifetime, nocapture would be set here, although the pointer does escape.

I assume we don't want to have UB in safe code, right? That would mean that we need to perform some analysis on the function body to determine which attributes we can emit and that we need to emit those attributes in the metadata, because other crates can't necessarilly perform this analysis, because they don't have access to the function body at all.

@shepmaster

This comment has been minimized.

Copy link
Member

shepmaster commented May 29, 2015

What are the benefits of applying the nocapture attribute when it is valid?

@ranma42

This comment has been minimized.

Copy link
Contributor

ranma42 commented Dec 27, 2015

LLVM seems to be inferring the nocapture attribute in the functionattrs pass. I think that, thank to lifetimes, the analysis performed in Rust could be more accurate than that performed in LLVM, but for trivial examples LLVM computes the attribute as desired.

@dotdash

This comment has been minimized.

Copy link
Contributor Author

dotdash commented Dec 27, 2015

@ranma LLVM can't do that for external functions though. We'd still have to store the information in the crate metadata, even if we don't perform the analysis ourselves and just inspect what LLVM could infer.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 1, 2017

In the absence of clearer unsafe code guidelines, it is not clear when we should add this attribute anyhow.

@brson brson added A-LLVM P-low and removed A-LLVM labels Jun 1, 2017

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jun 1, 2017

Wishlist. If somebody ever figures it out, great.

@brson brson closed this Jun 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.