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

typeck: extract expr type-checking to expr.rs + refactor check_expr_kind #61857

Merged
merged 22 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6cf4b3a
typeck: check_expr_kind -> expr.rs
Centril Jun 14, 2019
7227a38
typeck/expr.rs: extract out check_expr_box.
Centril Jun 14, 2019
840f3f6
typeck/expr.rs: extract out check_expr_unary.
Centril Jun 14, 2019
d5cc080
typeck/expr.rs: extract out check_expr_addr_of.
Centril Jun 14, 2019
9131f95
typeck/expr.rs: extract out check_expr_path.
Centril Jun 14, 2019
74dd65e
typeck/expr.rs: extract out check_expr_break.
Centril Jun 15, 2019
7a41cc1
typeck/expr.rs: extract out check_expr_return.
Centril Jun 15, 2019
af800c7
typeck/expr.rs: move check_expr_assign here.
Centril Jun 15, 2019
867ff1b
typeck/expr.rs: extract out check_expr_while.
Centril Jun 15, 2019
046cd90
typeck/expr.rs: extract out check_expr_loop.
Centril Jun 15, 2019
fe004da
typeck/expr.rs: extract out check_expr_cast.
Centril Jun 15, 2019
877d834
typeck/expr.rs: extract out check_expr_array.
Centril Jun 15, 2019
82cac15
typeck/expr.rs: extract out check_expr_repeat.
Centril Jun 15, 2019
bb93488
typeck/expr.rs: extract out check_expr_tuple.
Centril Jun 15, 2019
1aa068a
typeck/expr.rs: move check_expr_struct here.
Centril Jun 15, 2019
8da059b
typeck/expr.rs: extract out check_expr_index.
Centril Jun 15, 2019
8fd2d12
typeck/expr.rs: extract out check_expr_yield.
Centril Jun 15, 2019
a551fe0
typeck/expr.rs: move check_expr_with_expectation_and_needs here.
Centril Jun 15, 2019
18edf3e
typeck/expr.rs: move some check_expr_*s here.
Centril Jun 15, 2019
819c4f2
typeck/expr.rs: move some check_return_expr here.
Centril Jun 15, 2019
5ee36b7
typeck/expr.rs: move check_method_call here.
Centril Jun 15, 2019
5057552
typeck/expr.rs: move check_field + struct helpers here.
Centril Jun 15, 2019
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
175 changes: 92 additions & 83 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,89 +76,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
tcx.mk_unit()
}
ExprKind::Break(destination, ref expr_opt) => {
if let Ok(target_id) = destination.target_id {
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.
let opt_coerce_to = {
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
enclosing_breakables.find_breakable(target_id)
.coerce
.as_ref()
.map(|coerce| coerce.expected_ty())
};

// If the loop context is not a `loop { }`, then break with
// a value is illegal, and `opt_coerce_to` will be `None`.
// Just set expectation to error in that case.
let coerce_to = opt_coerce_to.unwrap_or(tcx.types.err);

// Recurse without `enclosing_breakables` borrowed.
e_ty = self.check_expr_with_hint(e, coerce_to);
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_unit();
cause = self.misc(expr.span);
}

// Now that we have type-checked `expr_opt`, borrow
// the `enclosing_loops` field and let's coerce the
// type of `expr_opt` into what is expected.
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
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);
} else {
assert!(e_ty.is_unit());
coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
}
} else {
// If `ctxt.coerce` is `None`, we can just ignore
// the type of the expresison. This is because
// either this was a break *without* a value, in
// which case it is always a legal type (`()`), or
// else an error would have been flagged by the
// `loops` pass for using break with an expression
// where you are not supposed to.
assert!(expr_opt.is_none() || self.tcx.sess.err_count() > 0);
}

ctxt.may_break = true;

// the type of a `break` is always `!`, since it diverges
tcx.types.never
} else {
// Otherwise, we failed to find the enclosing loop;
// this can only happen if the `break` was not
// inside a loop at all, which is caught by the
// loop-checking pass.
if self.tcx.sess.err_count() == 0 {
self.tcx.sess.delay_span_bug(expr.span,
"break was outside loop, but no error was emitted");
}

// We still need to assign a type to the inner expression to
// prevent the ICE in #43162.
if let Some(ref e) = *expr_opt {
self.check_expr_with_hint(e, tcx.types.err);

// ... except when we try to 'break rust;'.
// ICE this expression in particular (see #43162).
if let ExprKind::Path(QPath::Resolved(_, ref path)) = e.node {
if path.segments.len() == 1 &&
path.segments[0].ident.name == sym::rust {
fatally_break_rust(self.tcx.sess);
}
}
}
// There was an error; make type-check fail.
tcx.types.err
}

self.check_expr_break(destination, expr_opt.deref(), expr)
}
ExprKind::Continue(destination) => {
if destination.target_id.is_ok() {
Expand Down Expand Up @@ -725,4 +643,95 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

ty
}

fn check_expr_break(
&self,
destination: hir::Destination,
expr_opt: Option<&'tcx hir::Expr>,
expr: &'tcx hir::Expr,
) -> Ty<'tcx> {
let tcx = self.tcx;
if let Ok(target_id) = destination.target_id {
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.
let opt_coerce_to = {
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
enclosing_breakables.find_breakable(target_id)
.coerce
.as_ref()
.map(|coerce| coerce.expected_ty())
};

// If the loop context is not a `loop { }`, then break with
// a value is illegal, and `opt_coerce_to` will be `None`.
// Just set expectation to error in that case.
let coerce_to = opt_coerce_to.unwrap_or(tcx.types.err);

// Recurse without `enclosing_breakables` borrowed.
e_ty = self.check_expr_with_hint(e, coerce_to);
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_unit();
cause = self.misc(expr.span);
}

// Now that we have type-checked `expr_opt`, borrow
// the `enclosing_loops` field and let's coerce the
// type of `expr_opt` into what is expected.
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
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);
} else {
assert!(e_ty.is_unit());
coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
}
} else {
// If `ctxt.coerce` is `None`, we can just ignore
// the type of the expresison. This is because
// either this was a break *without* a value, in
// which case it is always a legal type (`()`), or
// else an error would have been flagged by the
// `loops` pass for using break with an expression
// where you are not supposed to.
assert!(expr_opt.is_none() || self.tcx.sess.err_count() > 0);
}

ctxt.may_break = true;

// the type of a `break` is always `!`, since it diverges
tcx.types.never
} else {
// Otherwise, we failed to find the enclosing loop;
// this can only happen if the `break` was not
// inside a loop at all, which is caught by the
// loop-checking pass.
if self.tcx.sess.err_count() == 0 {
self.tcx.sess.delay_span_bug(expr.span,
"break was outside loop, but no error was emitted");
}

// We still need to assign a type to the inner expression to
// prevent the ICE in #43162.
if let Some(ref e) = expr_opt {
self.check_expr_with_hint(e, tcx.types.err);

// ... except when we try to 'break rust;'.
// ICE this expression in particular (see #43162).
if let ExprKind::Path(QPath::Resolved(_, ref path)) = e.node {
if path.segments.len() == 1 &&
path.segments[0].ident.name == sym::rust {
fatally_break_rust(self.tcx.sess);
}
}
}
// There was an error; make type-check fail.
tcx.types.err
}
}
}
1 change: 1 addition & 0 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ This API is completely unstable and subject to change.
#![feature(rustc_diagnostic_macros)]
#![feature(slice_patterns)]
#![feature(never_type)]
#![feature(inner_deref)]
Copy link
Contributor

Choose a reason for hiding this comment

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

what's up with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Used in some places, e.g. https://github.com/rust-lang/rust/pull/61857/files#diff-4dca14d9f8d48a6af9ed414a126a9823R243 (just search for .deref() in expr.rs) to make things type-check :)

Copy link
Member

Choose a reason for hiding this comment

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

Is that an addition to Option? Damn, I think it's 1. confusing 2. we should be making progress on the coerce front instead of adding method hacks :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep; there's a PR to rename it to .as_deref(). It's much less of a hack than using .map(...). =P

Copy link
Member

Choose a reason for hiding this comment

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

We should probably never stabilize it and introduce the structural coercion already, hmpf.


#![recursion_limit="256"]

Expand Down