[rust2cpg] support impl X blocks.#6031
Conversation
| // 'impl' GenericParamList? ('const'? '!'? Type 'for')? Type WhereClause? | ||
| // AssocItemList | ||
| // TODO: support `impl X for Y` and remove the side-effects (storeInDiffGraph). | ||
| private def visitImpl(impl: Impl): Seq[Ast] = { |
There was a problem hiding this comment.
Does rust allow to specify multiple impl X decls for the same type?
If so, this implementation here would create multiple TypeDecls with the same fullName.
There was a problem hiding this comment.
It does -- and I don't know why I can no longer paste a proper link, but please see the "multiple impl blocks for the same type" in the current diff.
But that correctly reminds me: the TypeDecl creation actually happens outside (see the last paragraph of #6028). Well, not for structs declared in the source-code, but for external ones. Anyway, for the impl X for Y I was thinking of having an extra pass à la swifsrc2cpg's ExtensionPass so that these kind of changes only happen in one place, rather than potentially scattered inside RustVisitor.
There was a problem hiding this comment.
Oh, just one more thing came into my mind: is it possible to specify multiple impl X decls for the same type but spread over different files? Does that lead to multiple TypeDecls with the same fullName?
There was a problem hiding this comment.
It's also fine (no duplicates), but it's good to have a test for it.
| code = code(selfParam), | ||
| index = 0, | ||
| isVariadic = false, | ||
| evaluationStrategy = EvaluationStrategies.BY_SHARING, |
There was a problem hiding this comment.
Rust has BY_VALUE parameter passing.
| |""".stripMargin) | ||
|
|
||
| "have correct fullName" in { | ||
| cpg.method.nameExact("bar").fullName.l shouldBe List("rust2cpgtest::Foo::bar") |
There was a problem hiding this comment.
rust2cpgtest is the module name right? If yes please it something like testModule or a like. The import part that should come across is that it is not the file name.
This this will affect lots of tests you should likely do that in a separate PR.
There was a problem hiding this comment.
The test fixture creates at least 2 files:
Cargo.toml, which is whererust2cpgtestcomes from.src/lib.rs(by default, sincecode(..)accepts a file name, and there's the usualmoreCodeto add more files) where the snippet is written into.
So, rust2cpgtest is not coming from the filename. Have I missed your point?
There was a problem hiding this comment.
I was aware of that. I was just suggesting that if you name the module testModule it is immediately obvious that this is not a file.
5e7b4ea to
43697f1
Compare
Only
impl Xmethods are handled here, and they become child methods to theXTYPE_DECL. I intend to remove the uglystoreInDiffGraphinside RustVisitor as I tackle theimpl X for Yvariant.