Skip to content

Fix never-type rvalue ICE #47746

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 2 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion src/librustc_mir/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
if let Some(expr) = expr {
unpack!(block = this.into(destination, block, expr));
} else {
this.cfg.push_assign_unit(block, source_info, destination);
let tcx = this.hir.tcx();
let ty = destination.ty(&this.local_decls, tcx).to_ty(tcx);
if ty.is_nil() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be sure I understand, the premise here is that the type is either () or else the block must diverge, since any other type would have yielded a compilation error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, with the current semantics, any other type should be impossible (i.e. any other type must be explicit and go through the Some(expr) branch instead) , because the only implicit return types are () and !.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we have a comment? That's a bit non-obvious. (If the code were like if ty.is_never() it'd be more obvious...)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. (The reason I opted for ty.is_nil() rather than ty.is_never() here is because it seemed less contextual — it's always true that we only want to assign a unit if the type is unit. One alternative to make it absolutely explicit would be to check for !ty.is_never() in an else clause and throw a warning there that it should be impossible, but this seems like overkill.)

// We only want to assign an implicit `()` as the return value of the block if the
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
this.cfg.push_assign_unit(block, source_info, destination);
}
}
// Finally, we pop all the let scopes before exiting out from the scope of block
// itself.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ExprKind::Continue { .. } |
ExprKind::Break { .. } |
ExprKind::InlineAsm { .. } |
ExprKind::Return {.. } => {
ExprKind::Return { .. } => {
unpack!(block = this.stmt_expr(block, expr));
this.cfg.push_assign_unit(block, source_info, destination);
block.unit()
Expand Down
46 changes: 46 additions & 0 deletions src/test/run-pass/never-type-rvalues.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(never_type)]
#![allow(dead_code)]
#![allow(path_statements)]
#![allow(unreachable_patterns)]

fn never_direct(x: !) {
x;
}

fn never_ref_pat(ref x: !) {
*x;
}

fn never_ref(x: &!) {
let &y = x;
y;
}

fn never_pointer(x: *const !) {
unsafe {
*x;
}
}

fn never_slice(x: &[!]) {
x[0];
}

fn never_match(x: Result<(), !>) {
match x {
Ok(_) => {},
Err(_) => {},
}
}

pub fn main() { }