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

Expand unbounded lifetime example code and improve wording #408

Merged
merged 2 commits into from
May 19, 2023

Conversation

Enselic
Copy link
Member

@Enselic Enselic commented May 9, 2023

Due to wrapping, the diff looks larger than it is. I have only changed

The most common source of this is dereferencing a raw pointer, which produces a reference with an unbounded lifetime.

into the more accurate

The most common source of this is taking a reference to a dereferenced raw pointer, which produces a reference with an unbounded lifetime.

See Zulip discussion.

I have also expanded on the example code by expanding:

For instance:

fn get_str<'a>() -> &'a str;

will produce an &str with an unbounded lifetime.

into:

For instance:

fn get_str<'a>(s: *const String) -> &'a str {
    unsafe { &*s }
}

fn main() {
    let soon_dropped = String::from("hello");
    let dangling = get_str(&soon_dropped);
    drop(soon_dropped);
    println!("Invalid str: {}", dangling); // Invalid str: gӚ_`
}

Enselic and others added 2 commits May 9, 2023 06:51
We can't guarantee the behavior when running this example, so it is not safe to run during tests.
Copy link
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I pushed a small change to mark the example as no_run since running code with UB in the tests could cause problems.

@ehuss ehuss merged commit b5f018f into rust-lang:master May 19, 2023
@Enselic Enselic deleted the unbounded-lifetime-example branch May 19, 2023 18:37
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 23, 2023
Update books

## rust-embedded/book

1 commits in d9eb4c3f75435b008881062ffa77bf0d1527b37d..f2aed2fe8e9f55508c86ba3aa4b6789b18a08a22
2023-05-12 12:26:37 UTC to 2023-05-12 12:26:37 UTC

- Add link to Comprehensive Rust 🦀 (rust-embedded/book#354)

## rust-lang/nomicon

2 commits in b5f7500fc40775096c2bbd204eae096612cf9047..b5f018fb5930cb733b0a8aaf2eed975d4771e74d
2023-05-19 18:10:25 UTC to 2023-05-14 20:22:16 UTC

- Expand unbounded lifetime example code and improve wording (rust-lang/nomicon#408)
- Update to 2021 edition (rust-lang/nomicon#410)

## rust-lang/reference

5 commits in 28dc0f3576b55f5e57c5d6e65cd68ba3161e9fd5..553d99b02a53b4133a40d5bd2e19958c67487c00
2023-05-22 10:50:07 UTC to 2023-05-10 01:54:07 UTC

- Update raw-dylib documentation for stabilization (rust-lang/reference#1345)
- fix a typo (rust-lang/reference#1359)
- fixed minor error in a comment that annotated output of a proc macro (rust-lang/reference#1358)
- Fix example code in `impl` docs (rust-lang/reference#1360)
- Add documentation for debugger_visualizer attribute (rust-lang/reference#1335)

## rust-lang/rustc-dev-guide

4 commits in 28dbeaf5c44bc7f5111ad412e99f2d7c5cec6c90..f1e637883fafeb83bdd5906ee7f467e4d35b7337
2023-05-18 02:44:05 UTC to 2023-05-11 09:59:23 UTC

- Add note about shell completion scripts (rust-lang/rustc-dev-guide#1686)
- filter out S-blocked issues in the "Getting Started" search (rust-lang/rustc-dev-guide#1687)
- Rename InstCombine to InstSimplify (rust-lang/rustc-dev-guide#1684)
- Use unpretty for debugging Hir (rust-lang/rustc-dev-guide#1685)
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.

2 participants