Closed spernsteiner closed 1 year ago
By the way, is there a reason the base branches for this and other PRs is different (in name) but equal (in commit) from the PR branch of the other PRs?
Keeps github from getting confused about what to show in the diff when I do more work (add commits and/or rebase) on the various branches or merge PRs out of order. I forget what exactly triggered this, but I definitely ran into some trouble along these lines with my previous batch of PRs.
I'm curious if you think the unlower module would be worth breaking out into its own crate and put on crates.io @spernsteiner, it seems useful in its own right
Rebased and addressed all comments
@kkysen https://github.com/immunant/c2rust/pull/839#issuecomment-1584110904
Actually, there seem to be issues with https://github.com/immunant/c2rust/pull/934 running on the string_casts.rs tests
Did you test string_casts.rs
on #934 alone, or on a merge of #934 and #839? I've rebased this branch (#934) onto master
(which now includes #839) and I haven't seen any test failures.
Did you test
string_casts.rs
on #934 alone, or on a merge of #934 and #839? I've rebased this branch (#934) ontomaster
(which now includes #839) and I haven't seen any test failures.
I merged #934 (this one) into #839 and tested it. If rebase #934 on master
, which now includes #839, works, then that's great and there's no issue at all.
This replaces
rewrite::expr::hir_op
with three new modules:rewrite::expr::unlower
: Builds the "unlowering map", which maps a MIR "precise location" (aLocation
plus aVec<SubLoc>
path) to information about the HIR that was lowered to produce that piece of the MIR. Specifically, it records theHirId
of the HIRExpr
that was lowered along with a description of how the MIR relates to the HIR. For example:For example:
For
f
, the unlowering map annotates the MIR as follows:The statement
_2 = _1
is associated with the expressiona
; the statement as a whole is storing the result of evaluatinga
into a MIR local, and the statement's rvalue_1
represents the expressiona
itself. Similarly,_0 = Add(move _2, const 1)
stores the result ofa + 1
into a local. If needed, we could extend theunlower
pass to also record thatmove _2
(a.k.a.bb0[2]
[Rvalue, RvalueOperand(0)]
) is lowered from theExpr
a
.On
g
, the unlowering map includes the following (among other entries):The call terminator
_4 = f(move _5)
computesf(y)
and stores the result into a local; its rvalue isf(y)
itself, and the first argument of the rvalue isy
.rewrite::expr::distribute
: Given the MIR rewrites produced bymir_op
and the unlowering map fromunlower
, this module distributes MIR rewrites to HIR nodes (identified by theirHirId
s). This step also checks for ambiguous case, such as multiple rewrites from different MIR locations being mapped to the same HIR node, where it's unclear in which order to apply the rewrites, and also detects MIR rewrites that couldn't be mapped to any HIR node.Using the example from above:
A MIR rewrite on
bb0[5]
[Rvalue, CallArg(0)]
would be attached to the MIRExpr
y
, and a rewrite onbb0[5]
[Rvalue
] would be attached tof(y)
. A MIR rewrite onbb0[5]
[]
(i.e. on the call terminator itself) would result in an error, since there is no good place in the HIR to attach such a rewrite.rewrite::expr::convert
: Converts theMirRewrite
s for each HIRExpr
intoSpan
-basedrewrite::Rewrite
s. These are returned as the final result ofexpr::gen_expr_rewrites
and are later applied to the input source code usingrewrite::apply
.Overall, this branch simplifies the handling of
SubLoc
s;distribute
treatsVec<SubLoc>
paths as mostly opaque (it expects each MIR rewrite'ssub_loc
field to exactly match an entry in the unlowering map), andconvert
doesn't deal withSubLoc
s at all. More preciseSubLoc
handling, which will be needed for some tricky pointer-to-pointer cases, goes in theunlower
module, whose sole purpose is establishing the mapping betweenSubLoc
s and HIR. Andunlower
makes the mapping between MIR and HIR explicit (unlikehir_op
, where it was computed implicitly while building rewrites), so it can be inspected for easier debugging.All test cases produce the same rewrites as before this branch, except for one place in
lighttpd-minimal
, where we now place aCell::from_mut
cast in a more appropriate location.