Skip to content

Remove vestigial diverging !-coercion #50351

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

Merged
merged 3 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
arm_span: arm.body.span,
source: match_src
});
coercion.coerce(self, &cause, &arm.body, arm_ty, self.diverges.get());
coercion.coerce(self, &cause, &arm.body, arm_ty);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 -- the fact that this parameter was still needed should have been a warning sign. sigh.

}
}

Expand Down
24 changes: 5 additions & 19 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
//! sort of a minor point so I've opted to leave it for later---after all
//! we may want to adjust precisely when coercions occur.

use check::{Diverges, FnCtxt, Needs};
use check::{FnCtxt, Needs};

use rustc::hir;
use rustc::hir::def_id::DefId;
Expand Down Expand Up @@ -798,22 +798,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
exprs: &[E],
prev_ty: Ty<'tcx>,
new: &hir::Expr,
new_ty: Ty<'tcx>,
new_diverges: Diverges)
new_ty: Ty<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
where E: AsCoercionSite
{
let prev_ty = self.resolve_type_vars_with_obligations(prev_ty);
let new_ty = self.resolve_type_vars_with_obligations(new_ty);
debug!("coercion::try_find_coercion_lub({:?}, {:?})", prev_ty, new_ty);

// Special-ish case: we can coerce any type `T` into the `!`
// type, but only if the source expression diverges.
if prev_ty.is_never() && new_diverges.always() {
debug!("permit coercion to `!` because expr diverges");
return Ok(prev_ty);
}

// Special-case that coercion alone cannot handle:
// Two function item types of differing IDs or Substs.
if let (&ty::TyFnDef(..), &ty::TyFnDef(..)) = (&prev_ty.sty, &new_ty.sty) {
Expand Down Expand Up @@ -1052,14 +1044,12 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
expression: &'gcx hir::Expr,
expression_ty: Ty<'tcx>,
expression_diverges: Diverges)
expression_ty: Ty<'tcx>)
{
self.coerce_inner(fcx,
cause,
Some(expression),
expression_ty,
expression_diverges,
None, false)
}

Expand All @@ -1085,7 +1075,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
cause,
None,
fcx.tcx.mk_nil(),
Diverges::Maybe,
Some(augment_error),
label_unit_as_expected)
}
Expand All @@ -1098,7 +1087,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
cause: &ObligationCause<'tcx>,
expression: Option<&'gcx hir::Expr>,
mut expression_ty: Ty<'tcx>,
expression_diverges: Diverges,
augment_error: Option<&mut FnMut(&mut DiagnosticBuilder)>,
label_expression_as_expected: bool)
{
Expand Down Expand Up @@ -1132,15 +1120,13 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
exprs,
self.merged_ty(),
expression,
expression_ty,
expression_diverges),
expression_ty),
Expressions::UpFront(ref coercion_sites) =>
fcx.try_find_coercion_lub(cause,
&coercion_sites[0..self.pushed],
self.merged_ty(),
expression,
expression_ty,
expression_diverges),
expression_ty),
}
}
} else {
Expand Down
32 changes: 10 additions & 22 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
/// you get indicates whether any subexpression that was
/// evaluating up to and including `X` diverged.
///
/// We use this flag for two purposes:
/// We currently use this flag only for diagnostic purposes:
///
/// - To warn about unreachable code: if, after processing a
/// sub-expression but before we have applied the effects of the
Expand All @@ -556,16 +556,8 @@ pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
/// foo();}` or `{return; 22}`, where we would warn on the
/// `foo()` or `22`.
///
/// - To permit assignment into a local variable or other place
/// (including the "return slot") of type `!`. This is allowed
/// if **either** the type of value being assigned is `!`, which
/// means the current code is dead, **or** the expression's
/// diverging flag is true, which means that a diverging value was
/// wrapped (e.g., `let x: ! = foo(return)`).
///
/// To repeat the last point: an expression represents dead-code
/// if, after checking it, **either** its type is `!` OR the
/// diverges flag is set to something other than `Maybe`.
/// An expression represents dead-code if, after checking it,
/// the diverges flag is set to something other than `Maybe`.
diverges: Cell<Diverges>,

/// Whether any child nodes have any type errors.
Expand Down Expand Up @@ -2999,8 +2991,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
&self.cause(return_expr.span,
ObligationCauseCode::ReturnType(return_expr.id)),
return_expr,
return_expr_ty,
self.diverges.get());
return_expr_ty);
}


Expand Down Expand Up @@ -3031,13 +3022,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let mut coerce: DynamicCoerceMany = CoerceMany::new(coerce_to_ty);

let if_cause = self.cause(sp, ObligationCauseCode::IfExpression);
coerce.coerce(self, &if_cause, then_expr, then_ty, then_diverges);
coerce.coerce(self, &if_cause, then_expr, then_ty);

if let Some(else_expr) = opt_else_expr {
let else_ty = self.check_expr_with_expectation(else_expr, expected);
let else_diverges = self.diverges.get();

coerce.coerce(self, &if_cause, else_expr, else_ty, else_diverges);
coerce.coerce(self, &if_cause, else_expr, else_ty);

// We won't diverge unless both branches do (or the condition does).
self.diverges.set(cond_diverges | then_diverges & else_diverges);
Expand Down Expand Up @@ -3722,7 +3713,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
hir::ExprBreak(destination, ref expr_opt) => {
if let Some(target_id) = destination.target_id.opt_id() {
let (e_ty, e_diverges, cause);
let (e_ty, cause);
if let Some(ref e) = *expr_opt {
// If this is a break with a value, we need to type-check
// the expression. Get an expected type from the loop context.
Expand All @@ -3741,13 +3732,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

// Recurse without `enclosing_breakables` borrowed.
e_ty = self.check_expr_with_hint(e, coerce_to);
e_diverges = self.diverges.get();
cause = self.misc(e.span);
} else {
// Otherwise, this is a break *without* a value. That's
// always legal, and is equivalent to `break ()`.
e_ty = tcx.mk_nil();
e_diverges = Diverges::Maybe;
cause = self.misc(expr.span);
}

Expand All @@ -3758,7 +3747,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let ctxt = enclosing_breakables.find_breakable(target_id);
if let Some(ref mut coerce) = ctxt.coerce {
if let Some(ref e) = *expr_opt {
coerce.coerce(self, &cause, e, e_ty, e_diverges);
coerce.coerce(self, &cause, e, e_ty);
} else {
assert!(e_ty.is_nil());
coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
Expand Down Expand Up @@ -3964,7 +3953,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
for e in args {
let e_ty = self.check_expr_with_hint(e, coerce_to);
let cause = self.misc(e.span);
coerce.coerce(self, &cause, e, e_ty, self.diverges.get());
coerce.coerce(self, &cause, e, e_ty);
}
coerce.complete(self)
} else {
Expand Down Expand Up @@ -4375,8 +4364,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
coerce.coerce(self,
&cause,
tail_expr,
tail_expr_ty,
self.diverges.get());
tail_expr_ty);
} else {
// Subtle: if there is no explicit tail expression,
// that is typically equivalent to a tail expression
Expand Down
5 changes: 2 additions & 3 deletions src/test/compile-fail/coerce-to-bang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ fn call_foo_f() {
}

fn array_a() {
// Accepted: return is coerced to `!` just fine, and then `22` can be
// because we already diverged.
let x: [!; 2] = [return, 22];
// Return is coerced to `!` just fine, but `22` cannot be.
let x: [!; 2] = [return, 22]; //~ ERROR mismatched types
}

fn array_b() {
Expand Down