Repository navigation
Add const generic and lifetime support to kernel! - #436
Conversation
|
Is there an issue with "accepting" invalid code? The compiler will reject it anyway, so is there any point in us validating Rust syntax as well? For example, if I give the macro an invalid Rust function, will I get a better error message from the macro or from the compiler? E.g. kernel! {
fn foo<What am I?>(_: Avx2, a: f32) { }
} |
|
I admit this goes a bit over my head; I am having trouble imagining all possible substitutions and all possible ways this could break. I had Astra 6 take a crack at breaking it; it didn't find any soundness issues. It did flag two issues but those probably aren't worth addressing, these are non-soundness edge cases on deliberately malformed inputs. This was true even before this PR, but this code would really benefit from safety comments, explaining why this is actually safe. Something like "we never cast or transmute lifetimes so if we mess something up the code will simply fail to build in the worse case" (if that's true) or "the only |
|
@RunDevelopment Without parsing the generics properly we aren't sure what we're expanding in the And it's a lot harder than you might think to accept arbitrary blobs. |
|
@Shnatsel I have fixed the first concern of Astra, the second concern I think is fine to leave as is, it only causes a compilation error if you try to name your kernel |
|
We're inserting generics under the |
|
@Shnatsel As the safety comment says, we only expand However we also made sure that these identifiers are bound in that scope. We are inside a function: $vis fn $name $(<
$($lifetime $(: $lt_bound $(+ $lt_bounds)*)?,)*
$($(const $const_param: $const_ty),+)?
>)? (
$token: $crate::$level $(, $arg: $arg_ty)*
) $(-> $ret)? {Once again here all the parameters involved are plain |
|
Astra proposed a way to simplify the expression under Here's its commentary: https://gist.github.com/Shnatsel/23797a13a6093b78b5dd3cf1894301a1 |
|
@Shnatsel It's just an internal macro helper syntax change, doesn't affect soundness in any way. I think it's fine either way (applied or not). |
Shnatsel
left a comment
There was a problem hiding this comment.
Thank you!
Looks good to me, but I am not very proficient with macros so I'd appreciate if someone else could take a look.
|
Doesn't seem to noticeably impact build time either |
This PR allows the function in
kernel!to have lifetime and const generic arguments.Originally I also planned to add generic types and
whereclauses but these required more complicated TT-muncher macros to correctly parse and reject all invalid syntax. We can always extend with that later.To my knowledge the macro I've (re)written now only accepts legal Rust constructs, and maps those faithfully.
An earlier prototype also had type generics (without bounds) but ran into the following issue. It would need to be parsed as such:
with a later
but there's a problem with this. To truly get
constparams to forward correctly you need to expand them as{ N }instead ofN, otherwise a typeNtakes precedence in the case of a collision.