-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[WIP] Add const qualifier in FnSig (for const fn pointers) #74553
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @matthewjasper (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/librustc_middle/ty/relate.rs
Outdated
a: ast::Constness, | ||
b: ast::Constness, | ||
) -> RelateResult<'tcx, ast::Constness> { | ||
if a == b { Ok(a) } else { Ok(ast::Constness::NotConst) } |
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 should be if a == b { Ok(a) } else { Err(...) }
, coercing from const fn()
to fn()
and from a const function item to a normal function pointer should be handled by adding a new variant to PointerCast
in src/librustc_middle/ty/adjustment.rssrc/librustc_middle/ty/adjustment.rs
and using it as appropriate in src/librustc_typeck/check/coercion.rs
.
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 added 2 types of pointer casting: NotConstFnPointer (const fn() => fn()), UnsafeNotConstFnPointer (const fn() => unsafe fn()) becase I can not find a better solution. But I think that it is not a good.
☔ The latest upstream changes (presumably #69749) made this pull request unmergeable. Please resolve the merge conflicts. |
Can I ask? What's the best way for implementation of implicit casting? |
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.
You should be able to chain an Unsafe
and a NotConst
coercion when you would use UnsafeNotConstFnPointer
.
I try to add implicit casting for drop const qualifier: const fn foo() { }
fn bar() { }
fn main() {
let x = if true { bar } else { foo };
} What's the best way for implementing this feature? How can I do ReifyFnPointer to not const fn pointer in this situation? |
I added an example of how can I do implicit cast for drop const qualificator. Is it good? Now I need edit check_match for this situation: const fn foo() {}
const fn bar() {}
fn baz() {}
fn main() {
let x = match 2 {
2 => foo,
3 => bar,
_ => baz
};
} |
☔ The latest upstream changes (presumably #74877) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #73851) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #75797) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #74862) made this pull request unmergeable. Please resolve the merge conflicts. |
@filtsin Ping from triage, any updates on this? |
I've no idea how can I resolve this question #74553 (comment) |
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've left some comments for how to get cases like that working correctly.
@@ -925,7 +984,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
// We have a LUB of prev_ty and new_ty, just return it. |
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 patterns in the match on (&prev_ty.kind, &new_ty.kind)
should use &ty::FnDef(..) | &ty::FnPtr(..)
instead of &ty::FnDef(..)
} else { | ||
Adjust::Pointer(PointerCast::ReifyFnPointer) | ||
} | ||
} |
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 match and the next would be changed to return an Option
and an arm for ty::FnPtr(..)
should be added here. Some(Adjust::Pointer(PointerCast::NotConstFnPointer))
would be returned if prev_drop_const
, otherwise None
would be returned. The apply_adjustments
calls would be skipped if None
is returned from the match.
apply_adjustments
would need to be changed to handle the case where a NotConstFnPointer
adjustment is applied when there is already a ReifyFnPointer
adjustment (by merging them to a NotConstFnPointer
adjustment).
e90d74c
to
0b0be68
Compare
☔ The latest upstream changes (presumably #77135) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
☔ The latest upstream changes (presumably #77926) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
… Add pretty print
☔ The latest upstream changes (presumably #77685) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
cc @rust-lang/wg-const-eval |
ty::FnPtr(_) => { | ||
// No change to value | ||
self.write_immediate(*src, dest)?; | ||
} |
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.
Please merge these two new match arms into the existing one for UnsafeFnPointer
... seems unnecessary to duplicate that code.
ty::FnPtr(fn_sig) => { | ||
// At this point, we are calling a function by raw pointer because | ||
// we know that it is const | ||
if self.ccx.tcx.features().const_fn_pointer |
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 do you mean with "by raw pointer"? A raw pointer would be *const T
/*mut T
, and I do not think there is such a pointer here.
const _: const unsafe fn() = unsafe_fn; | ||
//~^ ERROR mismatched types | ||
|
||
const _: unsafe fn() = const_unsafe_fn; |
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 file is called "const_fn_ptr_cast" but it only seems to check coercions, not casts (via as
). Is that the same code in the compiler or should as
-casts be tested explicitly as well?
@filtsin Ping from triage, any updates on this? |
@filtsin thanks for taking the time to contribute. I have to close this due to inactivity. If you wish and you have the time you can open a new PR with these changes and we'll take it from there. Thanks |
Impl #63997
Add const qualifier in FnSig like
unsafe
orextern
.With this changes we can write something like this:
or we can work around of const traits: