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

Add a new lint that warns for pointers to stack memory #134218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

1c3t3a
Copy link
Contributor

@1c3t3a 1c3t3a commented Dec 12, 2024

This adds a new lint with level Warn to check for code like:

fn foo() -> *const i32 {
  let x = 42;
  &x
}

and produce a warning like:

error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^

This fixes #134215.

@rustbot
Copy link
Collaborator

rustbot commented Dec 12, 2024

r? @compiler-errors

rustbot has assigned @compiler-errors.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2024
@1c3t3a
Copy link
Contributor Author

1c3t3a commented Dec 12, 2024

I am aware that this may warn on existing code. I wonder what is a good strategy here? Should it be an Allow lint first and then be migrated to Warn as part of an edition? Or even stay Allow forever?

@compiler-errors
Copy link
Member

Is there a reason why this was implemented as a rust lint rather than a clippy suspicious lint or something?

I guess let's find the fallout of this lint

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 12, 2024
Add a new lint that warns for pointers to stack memory

This adds a new lint with level `Warn` to check for code like:

```rust
fn foo() -> *const i32 {
  let x = 42;
  &x
}
```

and produce a warning like:
```text
error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^
```
This fixes rust-lang#134215.
@bors
Copy link
Contributor

bors commented Dec 12, 2024

⌛ Trying commit 40eb5d7 with merge 6db1a6a7ebb2ad243051f3ff949f987dd70cde2b...

@1c3t3a
Copy link
Contributor Author

1c3t3a commented Dec 12, 2024

Is there a reason why this was implemented as a rust lint rather than a clippy suspicious lint or something?

This was completely my judgment, I am happy to change this if requested. My reasoning was that this is a pretty fundamental lint, since the code it lints should probably never exist? And looking at the list of rustc lints I thought it might be a good fit.

@rust-log-analyzer

This comment has been minimized.

compiler/rustc_lint/src/builtin.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/builtin.rs Outdated Show resolved Hide resolved
This adds a new lint with level `Warn` to check for code like:

```rust
fn foo() -> *const i32 {
  let x = 42;
  &x
}
```

and produce a warning like:
```text
error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^
```
@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 40eb5d7 to e162e89 Compare December 12, 2024 17:17
@1c3t3a
Copy link
Contributor Author

1c3t3a commented Dec 12, 2024

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Oops - I don't fully understand that error. The failure looks like in the .stderr file, but it still has unexpected errors that it doesn't match on... This even happens when passing --bless.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

@craterbot check

@craterbot
Copy link
Collaborator

🚨 Error: missing start toolchain

🆘 If you have any trouble with Crater please ping @rust-lang/infra!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@compiler-errors
Copy link
Member

@craterbot run mode=check-only start=master#8e37e151835d96d6a7415e93e6876561485a3354 end=try#6db1a6a7ebb2ad243051f3ff949f987dd70cde2b

@craterbot
Copy link
Collaborator

👌 Experiment pr-134218 created and queued.
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2024
@jyn514
Copy link
Member

jyn514 commented Dec 13, 2024

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)
Click to see the possible cause of the failure (guessed by this bot)

Oops - I don't fully understand that error. The failure looks like in the .stderr file, but it still has unexpected errors that it doesn't match on... This even happens when passing --bless.

bless will generate a .stderr file for you, but it will not update the original .rs file. compiletest is complaining because it expects each warning to be annotated with //~ WARN. see https://rustc-dev-guide.rust-lang.org/tests/ui.html#error-annotations

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 3c39f0f to f56a313 Compare December 13, 2024 15:49
@rustbot
Copy link
Collaborator

rustbot commented Dec 13, 2024

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from f56a313 to 80a2f21 Compare December 13, 2024 17:40
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the stack-memory-warning branch from 80a2f21 to 4e7732c Compare December 14, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-crater Status: Waiting on a crater run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint for returning a pointer to stack memory associated with a local variable
8 participants