I tried this code:
use syn::{parse::{Parse, ParseStream}};
use syn::{Attribute, Error, Expr, ExprLit, Ident, Lit, LitStr, Meta, Result, Token};
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
if let Meta::NameValue(name_value) = &attr.meta {
let path_idents = name_value.path.segments.iter().map(|segment| &segment.ident);
if itertools::equal(path_idents, path)
&& let Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) = &name_value.value
{
return Some(s.clone());
}
}
None
}
I am trying to get the instance of all functions called by a function at the Mir layer.
For some reason, the Instance list I detected is different when I detect this code in the entire project and directly detect this code separately, but the MIR of these two codes is the same (in a workspace and in a crate alone)
The problem I'm having is that when I detect the instance of the function being called inside parse_attr, I don't add the Len const parameter to ParamEnv, so I want to try to add it manually.
But I didn't find the corresponding interface to generate a ParamEnv or add Clauses
rustc --version --verbose
:
nightly-2024-10-09
bjorn3
November 12, 2024, 3:31pm
2
yu532758082:
I tried this code:
use syn::{parse::{Parse, ParseStream}};
use syn::{Attribute, Error, Expr, ExprLit, Ident, Lit, LitStr, Meta, Result, Token};
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
if let Meta::NameValue(name_value) = &attr.meta {
let path_idents = name_value.path.segments.iter().map(|segment| &segment.ident);
if itertools::equal(path_idents, path)
&& let Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) = &name_value.value
{
return Some(s.clone());
}
}
None
}
Are you sure that is the code you tried? Syn isn't used anywhere inside the compiler, let alone close to MIR.
sorry, the syn is not the point
I just want to know is there a way to merge ParamEnv ?
bjorn3
November 13, 2024, 6:56am
4
Not that I'm aware of. You shouldn't need to anyway.
opened 07:55AM - 13 Nov 24 UTC
C-bug
needs-triage
<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the … bug,
along with any information you feel relevant to replicating the bug.
-->
I tried this code:
```rust
fn parse_test<const LEN: usize>(path: [&'static str; LEN]) {
let target = ["doc"];
if target.iter().eq(&path) {
println!("get target str");
}
}
fn main() {
parse_test(["doc"]);
}
```
I am implementing an algorithm for function call detection,I identify function calls by iterating over the terminator in the MIR of the function body,recursively detect the functions called by each function
When I get an instance, I use this logic to detect the function it calls (callee)
```rust
fn callees_of<'tcx>(bcx: BtyCtxt<'_, 'tcx>, instance: ty::Instance<'tcx>) -> &'tcx Callees<'tcx> {
let param_env = bcx.tcx.param_env_reveal_all_normalized(instance.def_id());
let mir_body = bcx.instance_mir_expect(instance.def);
let callees = Callees::from_raw_mut(
bcx.tcx
.arena
.dropless
.alloc_from_iter(mir_body.basic_blocks.indices().map(|_| Default::default())),
);
... detect logic ...
}
```
I expected to see this happen: *Can correctly obtain the normalize instance*
Instead, this happened: *In the above example, the bounds of paramenv contains a Binder { value: ConstArgHasType(LEN/#0, usize), bound_vars: [] } When I try to normalize this instance (I think only after normalization can I determine what function it calls) rustc_trait_selection::traits::select::SelectionContext>::evaluate_predicate_recursively -> <rustc_middle::ty::sty::ParamConst>::find_ty_from_env Function not included in paramenvConstArgHasTypeThis type is panic*
### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->
`rustc --version --verbose`:
```
nightly-2024-10-09
```
<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>
```
<backtrace>
```
</p>
</details>
I want to know if my usage of getting ParamEnv is wrong, or I should merge ParamEnv and add Cosnt bound to ParamEnv (if so, I hope you can tell me how to merge ParamEnv)
I explained my problem in this issue. Please help me tell me how to solve the problem of ConstArgHasType passing.