Skip to content

Commit

Permalink
rust: Prefer primary spans
Browse files Browse the repository at this point in the history
This would be great improvement to the current situation, where the
first span is used, which is quite often not the right one.

See rust-lang/rust.vim#174

Related to: neomake#2075
  • Loading branch information
dpc authored and blueyed committed Sep 9, 2018
1 parent 6ab44de commit b8f6f21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions autoload/neomake/makers/ft/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function! neomake#makers#ft#rust#cargo() abort
return maker
endfunction

" NOTE: does not use process_json, since cargo outputs multiple JSON root
" elements per line.
function! neomake#makers#ft#rust#CargoProcessOutput(context) abort
let errors = []
for line in a:context['output']
Expand Down Expand Up @@ -64,6 +66,13 @@ function! neomake#makers#ft#rust#CargoProcessOutput(context) abort
endif

let span = data.spans[0]
for candidate_span in data.spans
if candidate_span.is_primary
let span = candidate_span
break
endif
endfor

let expanded = 0
let has_expansion = type(span.expansion) == type({})
\ && type(span.expansion.span) == type({})
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/rust/cargo_error_primary_span.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"message":{"children":[],"code":{"code":"E0061","explanation":"\nThe number of arguments passed to a function must match the number of arguments\nspecified in the function signature.\n\nFor example, a function like:\n\n```\nfn f(a: u16, b: &str) {}\n```\n\nMust always be called with exactly two arguments, e.g. `f(2, \"test\")`.\n\nNote that Rust does not have a notion of optional function arguments or\nvariadic functions (except for its C-FFI).\n"},"level":"error","message":"this function takes 1 parameter but 2 parameters were supplied","rendered":"error[E0061]: this function takes 1 parameter but 2 parameters were supplied\n --> build/main.rs:6:5\n |\n1 | fn foo(i : i32) {\n | --------------- defined here\n...\n6 | foo(1, 1);\n | ^^^^^^^^^ expected 1 parameter\n\n","spans":[{"byte_end":15,"byte_start":0,"column_end":16,"column_start":1,"expansion":null,"file_name":"build/main.rs","is_primary":false,"label":"defined here","line_end":1,"line_start":1,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":16,"highlight_start":1,"text":"fn foo(i : i32) {"}]},{"byte_end":67,"byte_start":58,"column_end":14,"column_start":5,"expansion":null,"file_name":"build/main.rs","is_primary":true,"label":"expected 1 parameter","line_end":6,"line_start":6,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":14,"highlight_start":5,"text":" foo(1, 1);"}]}]},"package_id":"testfoo 0.1.0 (path+file:///home/dpc/tmp/testfoo)","reason":"compiler-message","target":{"crate_types":["bin"],"edition":"2015","kind":["bin"],"name":"testfoo","src_path":"/home/dpc/tmp/testfoo/build/main.rs"}}
{"message":{"children":[],"code":null,"level":"error","message":"aborting due to previous error","rendered":"error: aborting due to previous error\n\n","spans":[]},"package_id":"testfoo 0.1.0 (path+file:///home/dpc/tmp/testfoo)","reason":"compiler-message","target":{"crate_types":["bin"],"edition":"2015","kind":["bin"],"name":"testfoo","src_path":"/home/dpc/tmp/testfoo/build/main.rs"}}
{"message":{"children":[],"code":null,"level":"","message":"For more information about this error, try `rustc --explain E0061`.","rendered":"For more information about this error, try `rustc --explain E0061`.\n","spans":[]},"package_id":"testfoo 0.1.0 (path+file:///home/dpc/tmp/testfoo)","reason":"compiler-message","target":{"crate_types":["bin"],"edition":"2015","kind":["bin"],"name":"testfoo","src_path":"/home/dpc/tmp/testfoo/build/main.rs"}}
16 changes: 16 additions & 0 deletions tests/ft_rust.vader
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ Execute (cargo error without span):
AssertEqual [], getloclist(0)
bwipe

Execute (cargo: uses primary span):
new
file build/main.rs
let cargo = NeomakeTestsGetMakerWithOutput('neomake#makers#ft#rust#cargo',
\ 'tests/fixtures/rust/cargo_error_primary_span.json')
let cargo.cwd = getcwd()

CallNeomake 1, [cargo]

AssertEqualQf getloclist(0), [{
\ 'lnum': 6, 'bufnr': bufnr('%'), 'col': 5, 'valid': 1, 'vcol': 0, 'nr': 49,
\ 'type': 'E', 'pattern': '',
\ 'text': 'this function takes 1 parameter but 2 parameters were supplied: expected 1 parameter',
\ }]
bwipe

Execute (rust: cargo: uses directory with Cargo.toml as cwd):
let maker = neomake#makers#ft#rust#cargo()
AssertEqual maker.cwd, '%:p:h'
Expand Down

0 comments on commit b8f6f21

Please sign in to comment.