-
Notifications
You must be signed in to change notification settings - Fork 104
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
Improve UI/UX of post-monomorphization errors #1268
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add UI tests for these? We honestly should have had them all along.
src/macros.rs
Outdated
/// Asserts at compile time that `$condition` is true for `Self` or the given | ||
/// `$tyvar`s. | ||
macro_rules! ct_assert { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to name this (and phrase the doc comment) so that it's clear why it's different from const_assert
(immediately above in this file)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this name will appear in compiler errors, so we want it to be both externally intelligible and succinct.
I can definitely improve the documentation, but would welcome suggestions of alternative names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I suppose static_assert
might be fitting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/macros.rs
Outdated
(Self $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )? => $condition:expr) => { | ||
trait ConstAssert { | ||
const ASSERT: bool; | ||
} | ||
|
||
impl<T $(: $(? $optbound +)* $($bound +)*)?> ConstAssert for T { | ||
const ASSERT: bool = { | ||
const_assert!($condition); | ||
$condition | ||
}; | ||
} | ||
|
||
const_assert!(<Self as ConstAssert>::ASSERT); | ||
}; | ||
($($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $condition:expr) => { | ||
trait ConstAssert { | ||
const ASSERT: bool; | ||
} | ||
|
||
impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?,)*> ConstAssert for ($($tyvar,)*) { | ||
const ASSERT: bool = { | ||
const_assert!($condition); | ||
$condition | ||
}; | ||
} | ||
|
||
const_assert!(<($($tyvar,)*) as ConstAssert>::ASSERT); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap these bodies in an extra set of curly braces? Otherwise ConstAssert
is introduced into the caller's namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/macros.rs
Outdated
($tyvar:ident) => { | ||
use crate::KnownLayout; | ||
ct_assert!($tyvar: ?Sized + KnownLayout => { | ||
let dst_is_zst = match $tyvar::LAYOUT.size_info { | ||
crate::SizeInfo::Sized { .. } => false, | ||
crate::SizeInfo::SliceDst(TrailingSliceLayout { elem_size, .. }) => { | ||
elem_size == 0 | ||
} | ||
}; | ||
!dst_is_zst | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap this in an extra set of curly braces? Otherwise, use crate::KnownLayout;
is introduced into the caller's namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot, because |
Oh look, not the first time we've run into this issue. Bummer. |
2a433d4
to
b2cb6b7
Compare
Still need a resolution on https://github.com/google/zerocopy/pull/1268/files#r1601997671. |
bdba67b
to
6cfde91
Compare
src/macros.rs
Outdated
|
||
/// Assert at compile time that `tyvar` does not have a zero-sized DST | ||
/// component. | ||
macro_rules! ct_assert_dst_is_not_zst { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update this name?
src/macros.rs
Outdated
/// Asserts at compile time that `$condition` is true for `Self` or the given | ||
/// `$tyvar`s. Unlike `const_assert`, this is *strictly* a compile-time check. | ||
/// The condition is checked after monomorphization and, upon failure, emits a | ||
/// compile error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const_assert!
is also strictly a compile-time check so long as it's always called from const
code; its fanciness has to do with its behavior on older toolchains that don't support explicitly panicking in const
code. Maybe I should update the docs to be more clear about that.
The distinction is actually that static_assert
uses post-monomorphization errors only, and so its errors are generated during a different compiler phase. Arguably we could just inline static_assert
into ct_assert_dst_is_not_zst
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made additional clarifications both in the docs of const_assert
and static_assert
.
By asserting in a macro, rather than a helper function, rustc's diagnostics point to the user's callsite, rather than our own.
By asserting in a macro, rather than a helper function, rustc's diagnostics point to the user's callsite, rather than our own.