Skip to content

Fix codegen of never return functions - #1062

Merged
celinval merged 3 commits into
model-checking:mainfrom
celinval:issue-648
Apr 18, 2022
Merged

celinval merged 3 commits into
model-checking:mainfrom
celinval:issue-648

Conversation

@celinval

@celinval celinval commented Apr 18, 2022 •

Copy link
Copy Markdown
Contributor

Description of changes:

I have removed the Nevers hook and I have also introduced two stubs in our std library to provide more user friendly errors. Functions overriden: std::process::{abort, exit}

Resolved issues:

Resolves #648 and resolves #751.

Call-outs:

I did add a tiny fix to a wrong PropertyClass that I noticed while working on this fix.

Testing:

  • How is this change tested? New test

  • Is this a refactor change? No

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

I have removed the Nevers hook and I have also introduced two stubs in
our std library to provide more user friendly errors
(std::process::{abort, exit})
@celinval
celinval requested a review from a team as a code owner April 18, 2022 21:14

@tedinski tedinski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think just a style thing: I prefer we use match instead of an if let as an expression.

Comment thread library/std/src/lib.rs
pub use std::*;

// Override process calls with stubs.
pub mod process;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I vaguely recall us having issues with overriding things here that weren't macros. Was that resolved somehow?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The one I recall failing to override was std::mem::swap, which turned out was because we have a hook for it:

Comment on lines +60 to +63
// Transmuting to an uninhabited type is UB.
"transmute" => self.codegen_fatal_error(
PropertyClass::UnsupportedConstruct,
"transmuting to uninhabited type",
PropertyClass::DefaultAssertion,
"transmuting to uninhabited type has undefined behavior",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I made the wrong call helping Jai. Good catch. :)


let (p, target) = destination.unwrap();
let (place, target) =
if let Some((p, t)) = destination { (Some(p), Some(t)) } else { (None, None) };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: This boggled me. Can we convert to match instead of if let?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can. But why did it bother you?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think my brain treats if let as statement-like and match as expression-like. This is despite being happy with if as expression-like.

It's possible it's just me needing to get used to it. If you disagree, no problem.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It took me some time to get used to as well :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you don't mind, I prefer to move on with this PR since there's nothing else to change.

I don't have any strong opinion about this. I tend to favor if let when I'm looking for one specific match. It's less verbose and clear to me that it's an all or nothing situation. We can discuss this later and if one is preferred over the other, maybe we can have a little convention page.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, that's fine, I prefer velocity over this nit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks

Comment on lines +496 to +500
let call_stmt = if let Some(place) = place {
self.codegen_expr_to_place(place, call).with_location(loc.clone())
} else {
call.as_stmt(loc.clone())
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: here too

if i == 2 {
// This should never happen.
process::abort();
process::exit(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment says test abort but now we're calling exit. Is there a reason for choosing either?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to increase test coverage

Description: "Function abort() was invoked"\
in function std::process::abort

Status: SUCCESS\

@zhassan-aws zhassan-aws Apr 18, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was expecting this to be UNREACHABLE, but it isn't because std::exit calls panic which we currently don't override. If we want it to be reported as UNREACHABLE (I think it should, but I'm not sure), then we can change the panic!(msg) to assert!(false, msg).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can't. It won't compile because of the never return. I think we should instrument panic inside the compiler to have a reach check.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see.


struct Nevers;

impl<'tcx> GotocHook<'tcx> for Nevers {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice to get rid of this!


let (p, target) = destination.unwrap();
let (place, target) =
if let Some((p, t)) = destination { (Some(p), Some(t)) } else { (None, None) };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It took me some time to get used to as well :)

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! This module introduces stubs for process methods.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

@adpaco adpaco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for getting rid of the Nevers hook!

Can you please check if this fixes #751 too?

@celinval

Copy link
Copy Markdown
Contributor Author

Thanks for getting rid of the Nevers hook!

Can you please check if this fixes #751 too?

Yes it does.

@celinval
celinval merged commit 6675fe1 into model-checking:main Apr 18, 2022
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 26, 2022
I have removed the Nevers hook and I have also introduced the following
stubs in our std library to provide more user friendly errors:
 - std::process::abort
 - std::process::exit
tedinski pushed a commit that referenced this pull request Apr 27, 2022
I have removed the Nevers hook and I have also introduced the following
stubs in our std library to provide more user friendly errors:
 - std::process::abort
 - std::process::exit
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.

Panic for assert_inhabited has no effect because of Nevers hook Codegen does not generate calls to functions that return the never type

4 participants