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

Allow coercions from never-type when ref binding is involved #118270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Expand Up @@ -131,7 +131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_expr_with_expectation(expr, ExpectHasType(expected))
}

fn check_expr_with_expectation_and_needs(
pub(super) fn check_expr_with_expectation_and_needs(
&self,
expr: &'tcx hir::Expr<'tcx>,
expected: Expectation<'tcx>,
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Expand Up @@ -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),
Copy link
Member

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?

Copy link
Member Author

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.

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);
};
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/pattern/issue-118113-ref-pattern-never-type.rs
@@ -0,0 +1,10 @@
// check-pass

pub struct Foo {
bar: u8
}

#[allow(unused_variables)]
fn main() {
let Foo { ref bar } = loop {};
}
22 changes: 22 additions & 0 deletions tests/ui/pattern/ref-pattern-assoc-type.rs
@@ -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();
}