-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow coercions from never-type when ref binding is involved #118270
Open
Aaron1011
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
Aaron1011:ref-pat-never-coerce
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1444,7 +1444,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||
// referent for the reference that results is *equal to* the | ||||||
// type of the place it is referencing, and not some | ||||||
// supertype thereof. | ||||||
let init_ty = self.check_expr_with_needs(init, Needs::maybe_mut_place(m)); | ||||||
let mut init_ty = self.check_expr_with_expectation_and_needs( | ||||||
init, | ||||||
ExpectHasType(local_ty), | ||||||
Needs::maybe_mut_place(m), | ||||||
); | ||||||
// The one exception to the above rule - we permit coercions when the expression has type ! | ||||||
// This allows `let Foo { ref my_field } = diverging_expr;`. The actual assignment is guaranteed | ||||||
// to be unreachable, so the soundness concerns with 'ref mut' do not apply. | ||||||
if init_ty.is_never() { | ||||||
init_ty = self.demand_coerce(init, init_ty, local_ty, None, AllowTwoPhase::No); | ||||||
}; | ||||||
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
|
||||||
|
||||||
if let Some(mut diag) = self.demand_eqtype_diag(init.span, local_ty, init_ty) { | ||||||
self.emit_type_mismatch_suggestions( | ||||||
&mut diag, | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// check-pass | ||
|
||
pub struct Foo { | ||
bar: u8 | ||
} | ||
|
||
#[allow(unused_variables)] | ||
fn main() { | ||
let Foo { ref bar } = loop {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// check-pass | ||
|
||
trait Call { | ||
type Out; | ||
fn call(self) -> Self::Out; | ||
} | ||
|
||
impl<F: FnOnce() -> T, T> Call for F { | ||
type Out = T; | ||
fn call(self) -> T { (self)() } | ||
} | ||
|
||
pub struct Foo { | ||
bar: u8 | ||
} | ||
|
||
fn diverge() -> ! { todo!() } | ||
|
||
#[allow(unused_variables)] | ||
fn main() { | ||
let Foo { ref bar } = diverge.call(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Huh, I wonder if this has subtle inference implications. Did you look into this?
Also, why did you add this?
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 the call made by
check_expr_coercible_to_type
(which we call in the non-bindings case) - I wanted to align them more closely.