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

Update vec! to have the same coercion behavior as arrays #58377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/liballoc/macros.rs
Expand Up @@ -39,9 +39,20 @@ macro_rules! vec {
($elem:expr; $n:expr) => (
$crate::vec::from_elem($elem, $n)
);
($($x:expr),*) => (
<[_]>::into_vec(box [$($x),*])
);
($($x:expr),*) => ({
// We use a temporary let binding to work around a coercion
// bug/strangness with `box [a, b]` syntax. Example:
//
// fn foo() {}
// fn bar() {}
//
// let _ = [foo, bar]; // works: fn items coerced to fn pointers
// let _ = <[_]>::into_vec(box [foo, bar]); // doesn't work
//
// See the `macro_fn_pointer_coercion` test.
let tmp = [$($x),*];
LukasKalbertodt marked this conversation as resolved.
Show resolved Hide resolved
<[_]>::into_vec(box tmp)
Copy link
Member

Choose a reason for hiding this comment

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

Possibly-silly question: now that there's a temporary, is box syntax gaining us anything over using Box::new? Is there a chance that some code that was making too-big-for-the-stack Vecs with this will no longer work?

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, is this something that should be fixed in the compiler about box syntax? Or is that dead enough that we should just stop ever using the keyword in the library?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I guess people could use vec! with data too big for the stack, so we shouldn't change this as it might break code. I don't think there is a way to easily fix that, so I will close this PR.

The correct way would be to fix box, I guess. But I heard it's not gonna land in this form anyway, so we might also just ignore this obscure function-pointer problem for now.

});
($($x:expr,)*) => (vec![$($x),*])
}

Expand Down
8 changes: 8 additions & 0 deletions src/liballoc/tests/vec.rs
Expand Up @@ -1151,3 +1151,11 @@ fn test_try_reserve_exact() {
}

}

#[test]
fn macro_fn_pointer_coercion() {
fn foo() {}
fn bar() {}

let _v = vec![foo, bar];
}