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

Add intrinsics::unreachable #16970

Merged
merged 2 commits into from
Oct 5, 2014
Merged

Conversation

kmcallister
Copy link
Contributor

I'm not sure how to add an automated test for this.

@thestinger
Copy link
Contributor

A simple run-pass test making use of it would be nice, even though it can't be directly tested.

@alexcrichton
Copy link
Member

What happens in a program like this?

use std::intrinsics;
fn main() {
    unsafe { intrinsics::unreachable(); }
}

@thestinger
Copy link
Contributor

That has undefined behaviour, it's the point of the intrinsic. It's the same thing as __builtin_unreachable with gcc and clang.

@kmcallister
Copy link
Contributor Author

My use case is something like

pub fn exit(n: uint) -> ! {
    static NR_EXIT: uint = 60;
    unsafe {
        asm!("syscall"
            :: "{rax}"(NR_EXIT), "{rdi}"(n));
        intrinsics::unreachable()
    }
}

which compiles on x86-64 Linux to just

0000000000000000 <_ZN4exit20he7def17266e9100bdaaE>:
   0:   b8 3c 00 00 00          mov    $0x3c,%eax
   5:   0f 05                   syscall 

Without the intrinsic I need something like loop { } to fulfill the bottom return type, which wastes 2 bytes in the object code, plus more for alignment.

@kmcallister
Copy link
Contributor Author

Not sure why the Travis build failed. The log shows rustc dying with SIGBUS.

@Kimundi
Copy link
Member

Kimundi commented Sep 4, 2014

Travis always fails its llvm 3.4 build atm. The 3.3 one succeeded though, which is the important one.

@huonw
Copy link
Member

huonw commented Sep 4, 2014

Does this avoid crashing horribly? (iirc there have been crashes due to LLVM assertions about unreachability previously)

fn foo() {
    unreachable();
    println!("foo");
}

@kmcallister
Copy link
Contributor Author

It doesn't crash rustc, no.

@kmcallister
Copy link
Contributor Author

@huonw, @thestinger: Any more thoughts about this?

@kmcallister
Copy link
Contributor Author

Any way I can get the contents of /c/bot/slave/auto-win64-64-nopt-t/build/src/test/run-make/intrinsic-unreachable/ without my own Windows machine? It may have failed just because of using test and wc in the Makefile; what kind of environment do these Makefiles use on Windows?

@alexcrichton
Copy link
Member

Not at this point sadly, the bots clean out all previous runs when they make a new build. You could update the test, however, to print it out and we can push to try.

@vadimcn
Copy link
Contributor

vadimcn commented Sep 20, 2014

@kmcallister: Please note that on Win64, the 'unreachable' instruction actually emits code.

@kmcallister
Copy link
Contributor Author

Aha!

@ghost
Copy link

ghost commented Sep 24, 2014

FWIW, matching an empty enum will produce an unreachable instruction:

use std::any::Void;
use std::intrinsics::transmute;

fn main() {
    let x: Void = unsafe { transmute(()) };
    match x {}
}
%"enum.core::any::Void<[]>[#3]" = type {}

; Function Attrs: uwtable
define internal void @_ZN4main20h31af98ebf3d86da3gaaE() unnamed_addr #0 {
entry-block:
  %x = alloca %"enum.core::any::Void<[]>[#3]"
  unreachable

so the unreachable intrinsic could be implemented in the library itself. Or not implemented at all if the above hack is deemed sufficient.


// See also src/test/run-make/intrinsic-unreachable.

fn f(x: uint) -> uint {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure whether tests should feature idiomatic code, but in this case I think it'd be better if you'd mark this function unsafe.

@kmcallister
Copy link
Contributor Author

Rebased and addressed @tbu-'s comment. r? @thestinger

bors added a commit that referenced this pull request Oct 5, 2014
I'm not sure how to add an automated test for this.
@bors bors closed this Oct 5, 2014
@bors bors merged commit 675aa76 into rust-lang:master Oct 5, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Apr 7, 2024
Fix tasks in tasks.json

rust-lang#16839 refactored the representation of tasks inside the VS Code extension. However, this data type is exposed to users, who can define their own tasks in the same format in `tasks.json` or `.code-workspace`.

Revert the data type to have a `command` field rather than a `program` field, and document the different fields. This code is also a little complex, so split out a `cargoToExecution` to handle the Task to Execution conversion logic.

After this change, any tasks.json with a `command` field works again. For example, the following tasks.json works as expected:

```
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cargo",
			"command": "build",
			"problemMatcher": [
			  "$rustc"
			],
			"group": "build",
			"label": "my example cargo build task"
		}
	]
}
```

Fixes rust-lang#16943 rust-lang#16949
lnicola pushed a commit to lnicola/rust that referenced this pull request Apr 20, 2024
Fix tasks in tasks.json

rust-lang#16839 refactored the representation of tasks inside the VS Code extension. However, this data type is exposed to users, who can define their own tasks in the same format in `tasks.json` or `.code-workspace`.

Revert the data type to have a `command` field rather than a `program` field, and document the different fields. This code is also a little complex, so split out a `cargoToExecution` to handle the Task to Execution conversion logic.

After this change, any tasks.json with a `command` field works again. For example, the following tasks.json works as expected:

```
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cargo",
			"command": "build",
			"problemMatcher": [
			  "$rustc"
			],
			"group": "build",
			"label": "my example cargo build task"
		}
	]
}
```

Fixes rust-lang#16943 rust-lang#16949
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants