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

Note difference in CTFE timing between associated and free constants #1120

Merged
merged 2 commits into from
Jan 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/items/associated-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we say explicitly anywhere that free constants are always evaluated, even when they are not used?

I went through Constant Evaluation and Constant items and could not find it explicitly stated. Some possible ways forward:

  1. reword this line to say "Associated constant definitions undergo constant evaluation only when referenced.", removing the reference to free constants
  2. mention that unused free constants always undergo CTFE in Constant items and reword this sentence to link to it.

Do you have any preferences or recommendations?

Copy link
Member

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.


### Associated Constants Examples

A basic example:
Expand Down Expand Up @@ -335,6 +338,24 @@ fn main() {
}
```

[Constant evaluation] timing:
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@XrXr XrXr Jan 24, 2022

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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