-
Notifications
You must be signed in to change notification settings - Fork 271
transpile: const macros: do not resolve expr type #1311
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
base: kkysen/expanded-translate-const-macros-conservative
Are you sure you want to change the base?
transpile: const macros: do not resolve expr type #1311
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.
Oh, I didn't realize you were working on this, too (I was as well). It's okay, though; the fix looks simple. As for the tests, I have some already in #1304.
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 don't think this works. It doesn't fix test_silk_int16_MIN
, the test I added for the buggy case from #853.
After cherry-picking: onto this branch and running pub const silk_int16_MIN: std::ffi::c_short = 0x8000 as std::ffi::c_int
as std::ffi::c_short;
#[no_mangle]
pub unsafe extern "C" fn test_silk_int16_MIN() -> std::ffi::c_int {
let mut _null: std::ffi::c_char = (*::core::mem::transmute::<
&[u8; 1],
&[std::ffi::c_char; 1],
>(b"\0"))[(silk_int16_MIN as std::ffi::c_int + 0x8000 as std::ffi::c_int) as usize];
return silk_int16_MIN as std::ffi::c_int;
} That looks sane to me (the const is -32768). Am I missing something? |
Hmm, that is correct. I just wasn't seeing that in the snapshot. If you rebase onto that branch and check in the updated snapshot, it should be all good. It just wasn't working for me for some reason. |
@fw-immunant, can I rebase this? |
…sion_test}` into `IndexMap`s instead of `HashMap`s Previously, `macro_invocations` was a `HashMap`, and thus iterating through it was unordered, which populated the `Vec<CExprId>` of `macro_expansions` non-deterministically, which then resulted in non-deterministic output from `--translate-const-macros conservative`. I also changed the other `macro_*` maps to `IndexMap`, as many other maps in `TypedAstContext` are already `IndexMap`s, too, and it's likely that we want these stably ordered and deterministic.
…ranslate-const-macros conservative` Some operations, such as ptr arithmetic, are `unsafe` and can be done in const macros. So as an initially overly conservative implementation, just make all `const`s use `unsafe` blocks in case `unsafe` operations are used. This is what we already do for `fn`s, for example, even if all of the operations in a `fn` are safe.
resolving the type ignores all casts, but some casts are nontrivial computations that we cannot elide, e.g. truncating larger types to smaller ones as far as I'm aware we can simply not resolve the expression type here; we'll emit more casts in these cases, but some of those may be load-bearing
1255ca4
to
d0c85d0
Compare
.resolve_expr_type_id(id) | ||
.unwrap_or((id, ty)); | ||
let expr = self.convert_expr(ctx, expr_id, None)?; | ||
let ty = self.ast_context[id].kind.get_type().unwrap_or(ty); |
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.
What's the difference between this and the above
let ty = self.ast_context[id]
.kind
.get_type()
.ok_or_else(|| format_err!("Invalid expression type"))?;
?
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, good catch--there really isn't one. I guess this can be dropped; all we needed to do was remove the unnecessary resolve_expr_type_id
.
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.
@fw-immunant, I'm not sure this is the right approach. It makes the translation much worse (see the snapshot diff).
604c58a
to
cdb7bd0
Compare
Here's my reasoning around this change:
We call this in If we avoid calling I'll look into whether there's some way to determine which casts are caused by the expansion context as opposed to being part of the macro itself, as the former as the only ones we should strip off to generate the translation for the macro body. |
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'll look into whether there's some way to determine which casts are caused by the expansion context as opposed to being part of the macro itself, as the former as the only ones we should strip off to generate the translation for the macro body.
If we're able to do that, that sounds like it would be good. Pulling in all the contextual casts not written in the macro itself, though, like this does, just seems like it results in a much worse translation, although I understand the reasoning for it.
Still needs tests.