Open Urgau opened 2 weeks ago
Maybe the error could be extended, to also say something along the lines of
"you must make sure that the variable you bind it to lives at least as long as the pointer
(in particular, if this pointer is returned from the current function, binding the String
inside the function will not suffice)"
That would already be a huge improvement over the current diagnostic.
Regarding the diagnostic, I think something like this should do it.
error: a dangling pointer will be produced because the temporary `CString` will be dropped
--> $DIR/calls.rs:32:29
|
LL | let ptr = cstring().as_ptr();
| --------- ^^^^^^ this pointer will immediately be invalid
| |
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
+ = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
+ = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
Note: I've slightly adjusted the wording to refer to the thing directly, instead of "it" or "the pointer"
I like it! The part I am most confused about is the (existing?) "pointers do not have a lifetime" -- it seems not directly related to the immediately next sentence? And it's not like the lifetime of a reference would magically make the referent live longer or so. I onder if it would be better to just remove that part entirely? And the part that indicates that "something referencing something" would influence the lifetime of anything. In fact I'd reword the entire first line:
= note: when calling `as_ptr` on a freshly constructed `CString`, it will be deallocated at the end of the statement
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
Sure, looks got to me. Let's see if someone wants to do it.
Steps:
help
to help_visit
and prefixing the new slugs with help_
.
Don't forget to update the current note as well!#[help(lint_NEW_SLUG_NAME)]
attributes where NEW_SLUG_NAME
is the name of the created slugs above.dangling_pointers_from_temporaries
lint tests in tests/ui/lint/dangling/
.
./x.py test --stage 1 tests/lint/ --bless
should do itStarting points can be found on the rustc-dev-guide.
Feel free to reach out (to me or others) here or in our Zulip.
@rustbot labels +E-easy +E-mentor
Could I please be assigned this issue? I think this would be a great learning opportunity for me!
Sure. @rustbot assign @Anthony-Eid
@Anthony-Eid you can even do that yourself, by writing @rustbot claim
:)
_Originally posted by @GrigorenkoPV in https://github.com/rust-lang/rust/pull/128985#discussion_r1750931393_