Git Product home page Git Product logo

Comments (7)

davidlattimore avatar davidlattimore commented on May 6, 2024

Sorry about the slow reply. The rule gets effectively inserted as a sub-module of the crate that you're operating on, so you should I think be able to refer to types the same way as you would in a sub-module.

from rerast.

jyn514 avatar jyn514 commented on May 6, 2024

Hmm that doesn't seem to be working.

Using an exported path:

error[E0432]: unresolved import `crate::Location`
 --> tests/__rerast_rules.rs:1:5
  |
1 | use crate::Location;
  |     ^^^^^^^^^^^^^^^ no `Location` in the root

Using absolute paths relative to the crate:

error[E0433]: failed to resolve: unresolved import
 --> tests/__rerast_rules.rs:1:12
  |
1 | use crate::data::prelude::Location;
  |            ^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `rcc::data`

Using absolute paths:

error[E0433]: failed to resolve: use of undeclared type or module `rcc`
 --> src/__rerast_rules.rs:1:5
  |
1 | use rcc::data::prelude::Location;
  |     ^^^ use of undeclared type or module `rcc`

from rerast.

davidlattimore avatar davidlattimore commented on May 6, 2024

I just tried with src/lib.rs containing:

pub mod data {
    pub mod prelude {
        pub struct Location {}

        impl Location {
            pub fn foo(&self) -> i32 { 41 }
            pub fn bar(&self) -> i32 { 42 }
        }
    }
}

pub struct Location2 {}

impl Location2 {
    pub fn foo(&self) -> i32 { 41 }
    pub fn bar(&self) -> i32 { 42 }
}

#[cfg(test)]
mod tests {
    use crate::data::prelude::Location;
    use crate::Location2;

    #[test]
    fn it_works() {
        let l = Location {};
        assert_eq!(l.foo(), 42);

        let l2 = Location2 {};
        assert_eq!(l2.foo(), 42);
    }
}

Then ran:

cargo +nightly rerast -p 'l1: crate::data::prelude::Location' -s 'l1.foo()' -r 'l1.bar()' --diff

This produced:

--- src/lib.rs
+++ src/lib.rs
@@ -24,7 +24,7 @@
     #[test]
     fn it_works() {
         let l = Location {};
-        assert_eq!(l.foo(), 42);
+        assert_eq!(l.bar(), 42);
 
         let l2 = Location2 {};
         assert_eq!(l2.foo(), 42);

from rerast.

jyn514 avatar jyn514 commented on May 6, 2024

That's really strange, your example doesn't work on my machine:

error[E0433]: failed to resolve: unresolved import
 --> src/__rerast_rules.rs:1:24
  |
1 | pub fn rule(l1: crate::data::prelude::Location){replace!(l1.foo() => l1.bar());}
  |                        ^^^^
  |                        |
  |                        unresolved import
  |                        help: a similar path exists: `import_test::data`

This is with 0.1.74.

from rerast.

jyn514 avatar jyn514 commented on May 6, 2024

Oh I found the problem! If I remove src/bin.rs it works. Rerast must be trying to fix both, but the import paths are different depending on which it tries to change.

from rerast.

jyn514 avatar jyn514 commented on May 6, 2024

As a workaround, I can use --file src/lib.rs and it will only try to fix the library, not the binary.

from rerast.

davidlattimore avatar davidlattimore commented on May 6, 2024

Ah, thanks for debugging what was going on there. That makes sense. Not sure what the best fix is there. Perhaps Rerast could be smart enough to swap between crate:: and the name of the crate depending on which context it's in.

from rerast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.