Is it possible to get context (related statements like definitions ) to an Expr?


#1

Hi, I am using the compiler plugin to instrument the Expr by modifying the ExtExt. However, the instrument code is based on the attributes (type information) of the Expr. e.g whether it is mutable or not and how it is defined.

I wonder if there is a method, to back transverse the AST tree, and get related statements about the Expr.

I thought it should be possible, because the compiler has a good knowledge of the code context to detect errors. An Idea, we can firstly resolve the structure of Expr, decide its ExprKind, an we may get an IdentId for the Expr. But can we resolve the type information (definition) by the NodeId?

Thanks for your attention.


#2

Is that a question? Expression has type, and type has mutability (~/git/rust/src/librustc/ty/sty.rs, see TypeAndMut)

Note that type is available in MIR, not ast.

I think there are no pointers going back from Expr. If you want them, you’ll need to build them yourself.


#3

Thanks for your answer, I understand now.

How it is defined

I mean how the value of the Expr is originated from. (like a variable p, how it is defined, like let p = 10;)