-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Add very basic "comptime" fn implementation #148820
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
base: main
Are you sure you want to change the base?
Changes from all commits
d843820
6ef6b1a
012ac6d
ea457c0
c77c2dc
af9bc67
3d02bbc
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 |
|---|---|---|
|
|
@@ -2264,10 +2264,12 @@ pub enum ConstContext { | |
| /// - Array length expressions | ||
| /// - Enum discriminants | ||
| /// - Const generics | ||
| /// | ||
| /// For the most part, other contexts are treated just like a regular `const`, so they are | ||
| /// lumped into the same category. | ||
| Const { inline: bool }, | ||
| Const { | ||
| /// For backwards compatibility `const` items allow | ||
| /// calls to `const fn` to get promoted. | ||
| /// We forbid that in comptime fns and inline consts. | ||
| allow_const_fn_promotion: bool, | ||
| }, | ||
| } | ||
|
|
||
| impl ConstContext { | ||
|
|
@@ -4235,16 +4237,21 @@ impl fmt::Display for Safety { | |
| #[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)] | ||
| #[derive(Default)] | ||
| pub enum Constness { | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// The function can only be called at compile-time | ||
| Always, | ||
| /// The function can be called both at runtime or compile-time | ||
| #[default] | ||
| Const, | ||
| NotConst, | ||
| Maybe, | ||
| /// The function can only be called at runtime | ||
| Never, | ||
| } | ||
|
|
||
| impl fmt::Display for Constness { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(match *self { | ||
| Self::Const => "const", | ||
| Self::NotConst => "non-const", | ||
| Self::Always => "comptime", | ||
| Self::Maybe => "const", | ||
| Self::Never => "non-const", | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -4284,7 +4291,7 @@ impl FnHeader { | |
| } | ||
|
|
||
| pub fn is_const(&self) -> bool { | ||
| matches!(self.constness, Constness::Const) | ||
| matches!(self.constness, Constness::Maybe) | ||
| } | ||
|
Comment on lines
4293
to
4295
Member
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. I think we might want to audit usages of this. Leave a FIXME? |
||
|
|
||
| pub fn is_unsafe(&self) -> bool { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1524,7 +1524,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
| } | ||
| if should_encode_constness(def_kind) { | ||
| let constness = self.tcx.constness(def_id); | ||
| self.tables.constness.set(def_id.index, constness); | ||
| self.tables.constness.set(def_id.index, constness) | ||
|
Member
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. ? (please revert)
Contributor
Author
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. Rebase fallout |
||
| } | ||
| if let DefKind::Fn | DefKind::AssocFn = def_kind { | ||
| let asyncness = tcx.asyncness(def_id); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -232,8 +232,9 @@ defaulted_enum! { | |||||||||||||
|
|
||||||||||||||
| defaulted_enum! { | ||||||||||||||
| hir::Constness { | ||||||||||||||
| ( Const ) | ||||||||||||||
| ( NotConst ) | ||||||||||||||
| ( Maybe ) | ||||||||||||||
| ( Never ) | ||||||||||||||
| ( Always ) | ||||||||||||||
|
Comment on lines
+235
to
+237
Member
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.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
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 is a bit sus
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.
What part? The rename or the inequality?
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.
The inequality. I feel like this should be if
== hir::Constness::NotConst?