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

Allow blocks in const expressions #14183

Closed
wants to merge 1 commit into from

Conversation

Kimundi
Copy link
Member

@Kimundi Kimundi commented May 13, 2014

Only blocks with tail expressions that are const expressions
and items are allowed.

Implements rust-lang/rfcs#71 (#14181)

StmtExpr(ref expr, _) => block_span_err(expr.span),
StmtSemi(ref semi, _) => block_span_err(semi.span),
StmtMac(..) => v.tcx.sess.span_bug(e.span,
"unexpanded statement macro in const?!")
Copy link
Member

Choose a reason for hiding this comment

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

I think macro_rules! macros may stick around un-expanded. Can you add a test case too make sure we don't ICE here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean macro_rules! itself, or macros generated by it? For the latter there is a testcase already.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, macro_rules! itself

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a testcase, doesn't seem to ICE. :)

Copy link
Member

Choose a reason for hiding this comment

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

Excellent, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Or actually, can you add another test with the macro tagged with #[macro_export]? Those seem to be the ones that stick around.

Copy link
Member Author

Choose a reason for hiding this comment

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

static BLOCK_MACRO_RULES_EXPORTED: uint = {
    #[macro_export]
    macro_rules! baz {
        () => (612)
    }
    baz!()
};

gives me the error

    /home/marvin/dev/rust/fork/rust/src/test/run-pass/const-block-item.rs:51:19: 51:20 error: expected item after attributes
    /home/marvin/dev/rust/fork/rust/src/test/run-pass/const-block-item.rs:51     #[macro_export]
                                                                                               ^

Am I doing something wrong?

Copy link
Member

Choose a reason for hiding this comment

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

Oh cool, looks like the parser doesn't even accept that currently (it wouldn't make much sense anyway I think). Thanks for checking!

Copy link
Member Author

Choose a reason for hiding this comment

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

I get the same error in a regular function as well:

pub fn main() {
    #[macro_export]
    macro_rules! baz {
        () => (612)
    }
}

Only blocks with tail expressions that are const expressions
and items are allowed.
bors added a commit that referenced this pull request May 14, 2014
Closes #14184 (std: Move the owned module from core to std)
Closes #14183 (Allow blocks in const expressions)
Closes #14176 (Add tests for from_bits.)
Closes #14175 (Replaced ~T by Box<T> in manual)
Closes #14173 (Implements Default trait for BigInt and BigUint)
Closes #14171 (Fix #8391)
Closes #14159 (Clean up unicode code in libstd)
Closes #14126 (docs: Add a not found page)
Closes #14123 (add a line to the example to clarify semantics)
Closes #14106 (Pretty printer improvements)
Closes #14083 (rustllvm: Add LLVMRustArrayType)
Closes #13957 (io: Implement process wait timeouts)
@bors bors closed this in #14187 May 14, 2014
lilyball added a commit to lilyball/rust that referenced this pull request May 18, 2014
Change `bytes!()` to return

    {
        static BYTES: &'static [u8] = &[...];
        BYTES
    }

This gives it the `'static` lifetime, whereas before it had an rvalue
lifetime. Until recently this would have prevented assigning `bytes!()`
to a static, as in

    static FOO: &'static [u8] = bytes!(1,2,3);

but rust-lang#14183 fixed it so blocks are now allowed in constant expressions
(with restrictions).

Fixes rust-lang#11641.
bors added a commit that referenced this pull request May 18, 2014
Change `bytes!()` to return

    {
        static BYTES: &'static [u8] = &[...];
        BYTES
    }

This gives it the `'static` lifetime, whereas before it had an rvalue
lifetime. Until recently this would have prevented assigning `bytes!()`
to a static, as in

    static FOO: &'static [u8] = bytes!(1,2,3);

but #14183 fixed it so blocks are now allowed in constant expressions
(with restrictions).

Fixes #11641.
lnicola pushed a commit to lnicola/rust that referenced this pull request Mar 13, 2023
minor: Try to improve the `rustfmt.overrideCommand` docs

Closes rust-lang#14078
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.

2 participants