add section about implied bounds#1261
Conversation
MatthewPeterKelly
left a comment
There was a problem hiding this comment.
Thanks for adding this documentation to support rust-lang/rust#100747. I'll plan to hold off merging that PR until this one lands (so that I can add a good reference).
| Only lifetime bounds are implied, trait bounds still have to be explicitly added. | ||
| This behavior may change in the future however. The following example still causes an error: | ||
| ```rust,compile_fail | ||
| use std::fmt::Debug; | ||
| struct IsDebug<T: Debug>(T); | ||
| // error[E0277]: `T` doesn't implement `Debug` | ||
| fn doesnt_specify_t_debug<T>(x: IsDebug<T>) {} | ||
| ``` |
There was a problem hiding this comment.
This provides helpful context - good idea to add it here.
|
(I don't have write access to the repo, so I think you'll need someone else to rubber stamp the PR). |
|
@Havvy - would you be able to do a quick review for this PR? |
Havvy
left a comment
There was a problem hiding this comment.
Reading through this, I feel like the prose is unnecessarily vague. And in fact, it fails to answer two key questions:
- What are all the places that lifetime bounds are inferred? As written, it looks like it is only for generics in functions. Are there others?
- How are the bounds chosen by looking at the types of the function parameters and output? In the simplest case, as shown by the example, a top level reference to a generic type gives said generic type a lifetime bound of the lifetime of the reference. Are there more complicated cases? If so, what are they?
--
The examples are good.
src/trait-bounds.md
Outdated
| This is inferred because the function signature contains the type `&'a T` which is | ||
| only valid if `T: 'a` holds. | ||
|
|
||
| Rust adds implied bounds for all inputs and outputs of functions. Inside of `requires_t_outlives_a` |
There was a problem hiding this comment.
When does it add implied bounds? You have a lot of good examples, but the actual rules for how the bounds are inferred are missing. I don't actually know the rules, so I can't be helpful here. If you don't either, I guess we can merge this and open an issue or we can find somebody who does know the rules. I do imagine it would be something like "For each parameter type and output type, if the type contains a reference to a generic type T (declared by the function item?), then the bounds with a lifetime 'a, then T: 'a is inferred."
There was a problem hiding this comment.
I guess we can merge this and open an issue
let's do that then. I think explaining this would require the reference to have a section about well-formedness requirements and I don't have the time to write that one rn. Lets open an issue and i can try to look into that one at a later point.
|
done 👍 |
|
👋 do you think we can go ahead and merge this? |
Co-authored-by: Ryan Scheel <Ryan.havvy@gmail.com>
compile_fail is a dangerous thing to use since it doesn't validate which error is generated (it could be something like a typo).
ehuss
left a comment
There was a problem hiding this comment.
Sure, I think this can go ahead and get merged. I'd agree with Havvy's comments that this seems a little informal, but I can understand how being more precise would take a ton more work.
Update books ## rust-lang/book 3 commits in 668c64760b5c7ea654facb4ba5fe9faddfda27cc..72187f5cd0beaaa9c6f584156bcd88f921871e83 2023-08-04 14:42:07 UTC to 2023-08-03 13:36:44 UTC - redirects: change link for the `#![no_std]` tutorial (rust-lang/book#3705) - [chpt10.2] - Small wording changes (rust-lang/book#3724) - Improve sentence (rust-lang/book#3725) ## rust-embedded/book 1 commits in 1e5556dd1b864109985d5871616ae6b9164bcead..99ad2847b865e96d8ae7b333d3ee96963557e621 2023-08-11 06:31:04 UTC to 2023-08-11 06:31:04 UTC - Fix a small typo in qemu.md (rust-embedded/book#359) ## rust-lang/nomicon 1 commits in 302b995bcb24b70fd883980fd174738c3a10b705..388750b081c0893c275044d37203f97709e058ba 2023-08-10 21:15:21 UTC to 2023-08-10 21:15:21 UTC - Document thiscall abi (rust-lang/nomicon#311) ## rust-lang/reference 10 commits in 1ea0178266b3f3f613b0fabdaf16a83961c99cdb..d43038932adeb16ada80e206d4c073d851298101 2023-08-12 19:07:28 UTC to 2023-07-16 20:12:46 UTC - Document thiscall abi (rust-lang/reference#1092) - add section about implied bounds (rust-lang/reference#1261) - Clearly specify the `instruction_set` effects (rust-lang/reference#1307) - Fix merge queue building twice. (rust-lang/reference#1383) - Clarify UB around immutability & mutation (rust-lang/reference#1385) - mention the extra const UB (rust-lang/reference#1273) - Operator expressions: make the note about division by zero clearer. (rust-lang/reference#1384) - Make unsafe keyword docs less confusing (rust-lang/reference#1379) - Say that division by zero for primitive types panics (rust-lang/reference#1382) - Add CI trigger for merge queues. (rust-lang/reference#1381) ## rust-lang/rust-by-example 3 commits in 8a87926a985ce32ca1fad1be4008ee161a0b91eb..07e0df2f006e59d171c6bf3cafa9d61dbeb520d8 2023-07-24 11:37:55 UTC to 2023-07-24 11:35:36 UTC - Added attribute unused_labels - fixed warning. (rust-lang/rust-by-example#1729) - more explanation about panic (rust-lang/rust-by-example#1728) - chore: add the portuguese version of this project to `readme.md` (rust-lang/rust-by-example#1727) ## rust-lang/rustc-dev-guide 31 commits in b5a12d9..b123ab4 2023-08-14 08:34:59 UTC to 2023-07-11 06:02:34 UTC - fix: stabilize debugger_visualizer (rust-lang/rustc-dev-guide#1766) - feat(part-5-intro): make "Part 5" obvious (rust-lang/rustc-dev-guide#1753) - Update from `#[warn_]` to `#[warning]` diagnostic attributes (rust-lang/rustc-dev-guide#1765) - Add RPITIT documentation (rust-lang/rustc-dev-guide#1764) - fix(visitor.md): fix a type name in a code sample (rust-lang/rustc-dev-guide#1762) - fix(name-resolution): remove unnecessary closing paranthesis (rust-lang/rustc-dev-guide#1760) - fix(macro-expansion.md): fix the article `an` to `a` (rust-lang/rustc-dev-guide#1759) - fix(serialization.md): fix the name of a derive macro (rust-lang/rustc-dev-guide#1756) - fix(serialization.md): add a necessary plural suffix (rust-lang/rustc-dev-guide#1757) - fix(salsa.md): add punctuation to prevent confusion (rust-lang/rustc-dev-guide#1754) - fix(salsa.md): remove duplicate "To Be" verb (rust-lang/rustc-dev-guide#1755) - feat(fuzzing.md): make `halfempty` word a link (rust-lang/rustc-dev-guide#1750) - fix(about.md): use `a` instead of `an` (rust-lang/rustc-dev-guide#1751) - refactor(git.md): make git-scm links clickable (rust-lang/rustc-dev-guide#1747) - fix(walkthrough.md) add a comma operator to eliminate ambiguity (rust-lang/rustc-dev-guide#1749) - fix(git.md): remove a confusing end of sentence character (rust-lang/rustc-dev-guide#1748) - refactor(profiling/with_perf): remove a wrong to be verb (rust-lang/rustc-dev-guide#1746) - refactor(tests/headers): remove duplicate list item (rust-lang/rustc-dev-guide#1745) - refactor(test/headers.md): make the meaning more obvious (rust-lang/rustc-dev-guide#1744) - refactor(tests/ui): remove unnecessary duplicate word (rust-lang/rustc-dev-guide#1743) - refactor(compiletest): remove unnecessary duplicate word (rust-lang/rustc-dev-guide#1742) - generic_arguments.md: substs -> GenericArgs (rust-lang/rustc-dev-guide#1741) - fix(suggested): remove an unnecessary and confusing statement (rust-lang/rustc-dev-guide#1739) - fix(how-to-build-and-run): fix a typo ("fromer" -> "former") (rust-lang/rustc-dev-guide#1736) - fix(how-to-build-and-run): remove a wrong paragraph (rust-lang/rustc-dev-guide#1735) - coverage code has moved (rust-lang/rustc-dev-guide#1728) - linked issue is closed (rust-lang/rustc-dev-guide#1729) - remove duplicate reference in about-this-guide.md (rust-lang/rustc-dev-guide#1734) - Explain more in depth what early and late bound generic parameters are (rust-lang/rustc-dev-guide#1732) - add section for normalization with the new solver (rust-lang/rustc-dev-guide#1731) - Improve cleanup-crew.md with an example post (rust-lang/rustc-dev-guide#1730)
cc https://github.com/rust-lang/rust/pull/100747/files#r962488178