Stream: t-compiler/const-eval

Topic: inspecting ADT types on RHS of a const item


pnkfelix (Oct 08 2019 at 19:33, on Zulip):

I'm poking at #62614 (just because I'm looking at other code related to structural_match)

pnkfelix (Oct 08 2019 at 19:38, on Zulip):

and I have to admit that I'm sort of flummoxed by something that I thought would be a relatively simple task

pnkfelix (Oct 08 2019 at 19:43, on Zulip):

My goal: I want to traverse the right-hand side of the const item, and check that any struct/enum I find there is structural_match.

pnkfelix (Oct 08 2019 at 19:43, on Zulip):

(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 ...)

pnkfelix (Oct 08 2019 at 19:53, on Zulip):

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 ByRefs and Allocations...

pnkfelix (Oct 09 2019 at 12:10, on Zulip):

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.)

RalfJ (Oct 09 2019 at 13:37, on Zulip):

@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.

RalfJ (Oct 09 2019 at 13:37, on Zulip):

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.

pnkfelix (Oct 09 2019 at 13:40, on Zulip):

yeah, the tree I was thinking of was the HIR for the const definition

pnkfelix (Oct 09 2019 at 13:40, on Zulip):

which I eventually did find

pnkfelix (Oct 09 2019 at 13:40, on Zulip):

and then reflecting on that has made me realize that, in the most general case, we cannot assume we have that available

RalfJ (Oct 09 2019 at 13:40, on Zulip):

oh, you mean the AST, pre MIR-generation. yeah sorry I couldn't help much there. glad you found the answer somewhere :D

pnkfelix (Oct 09 2019 at 13:41, on Zulip):

this const pattern stuff is a pretty amazing rabbit hole.

RalfJ (Oct 09 2019 at 13:43, on Zulip):

I can only guess.

RalfJ (Oct 09 2019 at 13:43, on Zulip):

I have avoided pattern matching code like the plague myself ;)

oli (Oct 10 2019 at 08:00, on Zulip):

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

pnkfelix (Oct 10 2019 at 09:50, on Zulip):

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.)

pnkfelix (Oct 10 2019 at 09:51, on Zulip):

@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.

pnkfelix (Oct 10 2019 at 09:51, on Zulip):

or maybe that's not what I actually need, if the visitor handles type lookup and field enumeration...

pnkfelix (Oct 10 2019 at 10:02, on Zulip):

(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 Structural)

pnkfelix (Oct 10 2019 at 10:03, on Zulip):

(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.)

pnkfelix (Oct 10 2019 at 12:06, on Zulip):

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 ...?

pnkfelix (Oct 10 2019 at 12:06, on Zulip):

lets see, who implements the rustc::interpret::visitor::Value trait ...

pnkfelix (Oct 10 2019 at 12:07, on Zulip):

OpTy and MPlaceTy ...

pnkfelix (Oct 10 2019 at 12:08, on Zulip):

ah it looks like I may be able to feed the ty::Const into eval_const_to_op to get an OpTy I could then visit...

oli (Oct 10 2019 at 17:38, on Zulip):

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

Last update: Nov 15 2019 at 20:55UTC