Stream: t-compiler/wg-rls-2.0

Topic: Work on "Create from usage" quickfixes


Timo Freiberg (Jun 10 2020 at 17:34, on Zulip):

Hi everyone, I'm slowly starting to work on https://github.com/rust-analyzer/rust-analyzer/issues/4563
I'm stuck on a (hopefully) trivial issue again, what can I do when a test fails with the following?
This time I checked that the test fixture doesn't have syntax errors etc ;)

---- diagnostics::tests::test_add_field_from_usage stdout ----
[crates/ra_ide/src/diagnostics.rs:132] record_lit.path().unwrap() = Path {
    syntax: PATH@41..44
      PATH_SEGMENT@41..44
        NAME_REF@41..44
          IDENT@41..44 "Foo"
    ,
}
thread 'diagnostics::tests::test_add_field_from_usage' panicked at '

Failed to lookup PATH@41..44 in this Semantics.
Make sure to use only query nodes, derived from this instance of Semantics.
root node:   SOURCE_FILE@0..158
known nodes:

The test looks like this:

    #[test]
    fn test_add_field_from_usage() {
        check_apply_diagnostic_fix(
            r"
            fn main() {
                Foo { bar: 3, baz: false};
            }
            struct Foo {
                bar: i32
            }
            ",
            r"
            fn main() {
                Foo { bar: 3, baz: false};
            }
            struct Foo {
                bar: i32,
                baz: bool
            }
            ",
        )
    }
Florian Diebold (Jun 10 2020 at 17:56, on Zulip):

I think that means you've got a bug in your code? basically, as the message says, you can only pass syntax nodes to Semantics if you derived them from a node you got from it. I'd check the backtrace

Timo Freiberg (Jun 10 2020 at 18:25, on Zulip):

hmm, i got the syntax from the same AstDatabase which the Semantics is based on. i'm gonna check the backtrace though.

Florian Diebold (Jun 10 2020 at 18:32, on Zulip):

that's not enough, you need to get it from a parse / expand / etc. call on the same Semantics object

Timo Freiberg (Jun 10 2020 at 18:41, on Zulip):

aw man, and Semantics::parse doesn't return the syntax errors which the diagnostics function uses :c

Florian Diebold (Jun 10 2020 at 18:47, on Zulip):

if you're getting the syntax node via AstDiagnostic::ast, you could add a method to Semantics that does the same thing. you just need to call cache with the root node and the correct HirFileId (which you can get from the diagnostic, I think)

Timo Freiberg (Jun 10 2020 at 19:35, on Zulip):

Ok, I hoped I wouldn't have to add a new method to Semantics, but if that's necessary :sweat_smile:
Thanks for the support :)

Last update: Sep 27 2020 at 12:30UTC