I'm poking at #62614 (just because I'm looking at other code related to structural_match)
and I have to admit that I'm sort of flummoxed by something that I thought would be a relatively simple task
My goal: I want to traverse the right-hand side of the const item, and check that any struct/enum I find there is
(actually maybe my problem is that I'm trying to walk the const-expression structure based on a
ty::Const, when what I should be doing is starting from the
def_id there ...)
nonetheless I was wondering if there is anyway to traverse the implicit tree structure of a
ty::Const like that, or if it has all been totally boiled away into
okay yes I think I figured out what to do. (There remain interesting questions; e.g. we may want to adopt different rules for consts from the current crate versus extern crates. But that can be resolved later.)
@pnkfelix which tree structure are you talking about? we have a "value visitor" which might be want you want -- but that is type-guided; there is no tree structure to values in rust if you discard the types.
Rust only has raw uninterpreted bytes in memory, and you can turn them into a tree by picking a type at which to interpret the bytes.
yeah, the tree I was thinking of was the HIR for the const definition
which I eventually did find
and then reflecting on that has made me realize that, in the most general case, we cannot assume we have that available
oh, you mean the AST, pre MIR-generation. yeah sorry I couldn't help much there. glad you found the answer somewhere :D
this const pattern stuff is a pretty amazing rabbit hole.
I can only guess.
I have avoided pattern matching code like the plague myself ;)
Oh yea, the only reasonable way to get at parts of a constant is via the value visitors, they implement all the logic for getting the types of fields and so on
hmm. Okay maybe I will try to use a value visitor. (I do have a type in hand, so the fact that they are type-guided should not be a problem.)
@oli can you point me at the specific trait definition(s)? And/or example usage of value visitor? I specifically want/need to traverse a value to identify all of the struct/variant constructors that would have been used to create that value.
or maybe that's not what I actually need, if the visitor handles type lookup and field enumeration...
(really I just want to traverse a view of the value that exposes its types, so that I can ask each ADT if it implements
(and this way you would be allowed to have an
enum with some variants that are not
Structural, just as long as no pattern uses a const that uses such a variant.)
Oh, I think I need to provide still more context: I'm trying to incorporate this into code that is currently using
tcx.const_eval to evaluate constants, so I end up with
ty::Const as my results to inspect. Do the visitors defined in
rustc_mir::interpret::visitor handle that? My cursory inspection indicates that it may want operands that have a bit more structure ...?
lets see, who implements the
rustc::interpret::visitor::Value trait ...
ah it looks like I may be able to feed the
eval_const_to_op to get an
OpTy I could then visit...
Yes, converting to an
OpTy first should be the right way ^TM This is done frequently for match checking in https://github.com/rust-lang/rust/blob/9c588c1e2391c20c156814f7ae118f62e96a3976/src/librustc_mir/const_eval.rs#L517