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

proposal: add compile-time version of unimplemented!() #39930

Closed
jonhoo opened this issue Feb 18, 2017 · 3 comments
Closed

proposal: add compile-time version of unimplemented!() #39930

jonhoo opened this issue Feb 18, 2017 · 3 comments

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Feb 18, 2017

Proposal: add compile-time checked version of unimplemented!()

We all know and love the unimplemented!() macro for keeping track of things we haven't yet gotten around to. However, it is only checked at runtime if a certain code path is encountered. This is fine in many cases, but when writing new code from scratch (especially when porting), refactoring large amounts of code, or building comprehensive new features, you often have segments of code that you haven't implemented yet, but you know you'll have to.

At least for me, it is common to write a chunk of code, but leaving out some annoying edge cases, or maybe logic (like generating a unique identifier) that I want to deal with alter. I know that I will have to complete that block of code before the program does something useful, but I want to do it later. This can be either because I want to complete the main logical flow of the code first, because I haven't figured out how to do that part yet, or simply because I want to see if the rest of my code compiles correctly.

It would be extremely useful to have a variant of unimplemented!() that would generate a compile-time error if present in my code. Something like incomplete!(). However, it should have some properties that I believe mean it can't be implemented without some help from the compiler:

  1. It should be usable as an expression of any type (similar to ! for unimplemented!())
  2. It should only error if the program compiles correctly. Or rather, it should not prevent any other errors from being printed. In essence, all passes of the compiler (including the borrow checker) should run despite the presence of incomplete!().
  3. It should not emit warnings about dead code or unused variables following it (which using unimplemented!() does).

The closest I've been able to come up with on my own is the following

macro_rules! incomplete {
    () => {{
        #[deny(non_snake_case)]
        let _mustBeImplemented = ();
        loop {}
    }}
}

This will only error out after all compiler passes (2), and since it returns !, it is sort of usable in place of any expression (1). However, it does not fit (3), because the infinite loop makes the compiler believe that all code following it is dead. Furthermore, the error it produces is obviously not appropriate for what the macro's intent is.

@sanmai-NL
Copy link

Thanks for the suggestion. This should be filed not under this repo, which is for the Rust compiler toolchain, but under rust-lang/rfcs. Was that unclear from the contribution guidelines?

@jonhoo
Copy link
Contributor Author

jonhoo commented Feb 19, 2017

I considered posting it under rfcs, but the saw #39916, which was similar in intent and not asked to post there. Furthermore, this isn't currently in a state where it could be made a PR, though I guess I could rewrite it as such if that would be better?

@jonhoo
Copy link
Contributor Author

jonhoo commented Feb 19, 2017

Following the recommendation in https://github.com/rust-lang/rfcs#before-creating-an-rfc, I've re-filed this as rust-lang/rfcs#1911. Thanks!

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

No branches or pull requests

2 participants