-
Notifications
You must be signed in to change notification settings - Fork 484
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
Note difference in CTFE timing between associated and free constants #1120
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,6 +293,9 @@ type that the definition has to implement. | |
An *associated constant definition* defines a constant associated with a | ||
type. It is written the same as a [constant item]. | ||
|
||
Unlike [free] constants, associated constant definitions undergo | ||
[constant evaluation] only when referenced. | ||
|
||
### Associated Constants Examples | ||
|
||
A basic example: | ||
|
@@ -335,6 +338,24 @@ fn main() { | |
} | ||
``` | ||
|
||
[Constant evaluation] timing: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I immediately read this, I didn't know what it meant by "timing". Instead of placing this down in the "examples" section, can this code block be moved up to the paragraph that you added above? Generally in the reference, when there is an example demonstrating some concept, we try to write that example immediately after the paragraph that introduces that concept. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that makes sense. Moved in be49428 |
||
|
||
```rust | ||
struct Struct; | ||
|
||
impl Struct { | ||
const ID: i32 = 1; | ||
// Definition not immediately evaluated | ||
const PANIC: () = panic!("compile-time panic"); | ||
} | ||
|
||
fn main() { | ||
assert_eq!(1, Struct::ID); | ||
// Referencing Struct::PANIC causes compilation error | ||
// let _ = Struct::PANIC; | ||
} | ||
``` | ||
|
||
[_ConstantItem_]: constant-items.md | ||
[_Function_]: functions.md | ||
[_MacroInvocationSemi_]: ../macros.md#macro-invocation | ||
|
@@ -362,3 +383,5 @@ fn main() { | |
[regular function parameters]: functions.md#attributes-on-function-parameters | ||
[generic parameters]: generics.md | ||
[where clauses]: generics.md#where-clauses | ||
[free]: ../glossary.md#free-item | ||
[constant evaluation]: ../const_eval.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems odd, to make a guarantee by stating its absence on associated consts. Do we say explicitly anywhere that free constants are always evaluated, even when they are not used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through Constant Evaluation and Constant items and could not find it explicitly stated. Some possible ways forward:
Do you have any preferences or recommendations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 sounds like something we will eventually want to do, but will probably require T-lang FCP to ensure that we are willing to commit to this as a guarantee.