Skip to content

Commit bb78041

Browse files
committed
Add support for 'required-features'.
Discover 'required-features' in Cargo manifest and initialize value of 'rust-cargo-features'.
1 parent 1ffbbfe commit bb78041

File tree

5 files changed

+90
-25
lines changed

5 files changed

+90
-25
lines changed

flycheck-rust.el

+14-8
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ FILE-NAME is the path of the file that is matched against the
119119
`src_path' value in the list of `targets' returned by `cargo
120120
read-manifest'.
121121
122-
Return a cons cell (KIND . NAME) where KIND is the target
123-
kind (lib, bin, test, example or bench), and NAME the target
124-
name (usually, the crate name). If FILE-NAME exactly matches a
125-
target `src-path', this target is returned. Otherwise, return
122+
Return an alist ((KIND . k) (NAME . n) (REQUIRED-FEATURES . rf))
123+
where KIND is the target kind (lib, bin, test, example or bench),
124+
NAME the target name (usually, the crate name), and REQUIRED-FEATURES is the
125+
optional list of features required to build the selected target. If FILE-NAME
126+
exactly matches a target `src-path', this target is returned. Otherwise, return
126127
the closest matching target, or nil if no targets could be found.
127128
128129
See http://doc.crates.io/manifest.html#the-project-layout for a
@@ -160,7 +161,10 @@ description of the conventional Cargo project layout."
160161
(--find (not (equal target it))))))
161162
(when target
162163
(let-alist target
163-
(cons (flycheck-rust-normalize-target-kind .kind) .name))))))
164+
(seq-filter (lambda (kv) (cdr kv))
165+
(list (cons 'kind (flycheck-rust-normalize-target-kind .kind))
166+
(cons 'name .name)
167+
(cons 'required-features .required-features))))))))
164168

165169

166170
(defun flycheck-rust-normalize-target-kind (kinds)
@@ -194,9 +198,11 @@ Flycheck according to the Cargo project layout."
194198
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
195199
(with-demoted-errors "Error in flycheck-rust-setup: %S"
196200
(-when-let* ((file-name (buffer-file-name))
197-
((kind . name) (flycheck-rust-find-cargo-target file-name)))
198-
(setq-local flycheck-rust-crate-type kind)
199-
(setq-local flycheck-rust-binary-name name))))
201+
(target (flycheck-rust-find-cargo-target file-name)))
202+
(let-alist target
203+
(setq-local flycheck-rust-features .required-features)
204+
(setq-local flycheck-rust-crate-type .kind)
205+
(setq-local flycheck-rust-binary-name .name)))))
200206

201207
(provide 'flycheck-rust)
202208

tests/crate-with-features/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "test-crate-with-features"
3+
version = "0.1.0"
4+
5+
[features]
6+
fea1 = []
7+
fea2 = []
8+
9+
[[bin]]
10+
name = "main"
11+
path = "src/main.rs"
12+
required-features = ["fea1"]
13+
14+
[[example]]
15+
name = "example"
16+
path = "example/example.rs"
17+
required-features = ["fea1", "fea2"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/crate-with-features/src/main.rs

Whitespace-only changes.

tests/test-rust-setup.el

+58-17
Original file line numberDiff line numberDiff line change
@@ -38,97 +38,138 @@
3838
(defun crate-file (file-name)
3939
(expand-file-name file-name "tests/test-crate"))
4040

41+
(defun crate-with-features-file (file-name)
42+
(expand-file-name file-name "tests/crate-with-features"))
43+
4144
(defun lib-crate-file (file-name)
4245
(expand-file-name file-name "tests/custom-lib-target"))
4346

4447
(defun build-script-crate-file (file-name)
4548
(expand-file-name file-name "tests/build-script-test"))
4649

50+
(defun cdrassoc (sym alist) (cdr (assoc sym alist)))
51+
52+
(defun get-cargo-version ()
53+
(let ((cargo (funcall flycheck-executable-find "cargo")))
54+
(with-temp-buffer
55+
(call-process cargo nil '(t nil) nil "--version")
56+
(buffer-string))))
57+
58+
(defun cargo-version ()
59+
(let* ((version-string-parts (split-string (get-cargo-version)))
60+
(version (cadr version-string-parts))
61+
(version-parts (split-string version "-")))
62+
(car version-parts)))
63+
4764
(describe
4865
"`flycheck-rust-find-cargo-target' associates"
4966

5067
(it "'src/lib.rs' to the library target"
5168
(expect
52-
(car (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
69+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
5370
:to-equal "lib"))
5471

5572
(it "'src/a.rs' to the library target"
5673
(expect
57-
(car (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
74+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
5875
:to-equal "lib"))
5976

6077
(it "'src/main.rs' to the main binary target"
6178
(expect
6279
(flycheck-rust-find-cargo-target (crate-file "src/main.rs"))
63-
:to-equal '("bin" . "test-crate")))
80+
:to-equal '((kind . "bin") (name . "test-crate"))))
6481

6582
(it "'src/bin/a.rs' to the 'a' binary target"
6683
(expect
6784
(flycheck-rust-find-cargo-target (crate-file "src/bin/a.rs"))
68-
:to-equal '("bin" . "a")))
85+
:to-equal '((kind . "bin") (name . "a"))))
6986

7087
(it "'src/bin/b.rs' to the 'b' binary target"
7188
(expect
7289
(flycheck-rust-find-cargo-target (crate-file "src/bin/b.rs"))
73-
:to-equal '("bin" . "b")))
90+
:to-equal '((kind . "bin") (name . "b"))))
7491

7592
(it "'src/bin/support/mod.rs' to any binary target"
7693
(expect
7794
(flycheck-rust-find-cargo-target (crate-file "src/bin/support/mod.rs"))
78-
:to-equal-one-of '("bin". "a") '("bin". "b")))
95+
:to-equal-one-of
96+
'((kind . "bin") (name . "a"))
97+
'((kind . "bin") (name . "b"))))
7998

8099
(it "'tests/a.rs' to the 'a' test target"
81100
(expect
82101
(flycheck-rust-find-cargo-target (crate-file "tests/a.rs"))
83-
:to-equal '("test" . "a")))
102+
:to-equal '((kind . "test") (name . "a"))))
84103

85104
(it "'tests/support/mod.rs' to any test target"
86105
(expect
87106
(flycheck-rust-find-cargo-target (crate-file "tests/support/mod.rs"))
88-
:to-equal-one-of '("test". "a") '("test". "b")))
107+
:to-equal-one-of
108+
'((kind . "test") (name . "a"))
109+
'((kind . "test") (name . "b"))))
89110

90111
(it "'examples/a.rs' to the 'a' example target"
91112
(expect
92113
(flycheck-rust-find-cargo-target (crate-file "examples/a.rs"))
93-
:to-equal '("example" . "a")))
114+
:to-equal '((kind . "example") (name . "a"))))
94115

95116
(it "'examples/b.rs' to the 'b' example target"
96117
(expect
97118
(flycheck-rust-find-cargo-target (crate-file "examples/b.rs"))
98-
:to-equal '("example" . "b")))
119+
:to-equal '((kind . "example") (name . "b"))))
99120

100121
(it "'examples/support/mod.rs' to any example target"
101122
(expect
102123
(flycheck-rust-find-cargo-target (crate-file "examples/support/mod.rs"))
103-
:to-equal-one-of '("example" . "a") '("example" . "b")))
124+
:to-equal-one-of
125+
'((kind . "example") (name . "a"))
126+
'((kind . "example") (name . "b"))))
104127

105128
(it "'benches/a.rs' to the 'a' bench target"
106129
(expect
107130
(flycheck-rust-find-cargo-target (crate-file "benches/a.rs"))
108-
:to-equal '("bench" . "a")))
131+
:to-equal '((kind . "bench") (name . "a"))))
109132

110133
(it "'benches/b.rs' to the 'b' bench target"
111134
(expect
112135
(flycheck-rust-find-cargo-target (crate-file "benches/b.rs"))
113-
:to-equal '("bench" . "b")))
136+
:to-equal '((kind . "bench") (name . "b"))))
114137

115138
(it "'benches/support/mod.rs' to any bench target"
116139
(expect
117140
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
118-
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
141+
:to-equal-one-of
142+
'((kind . "bench") (name . "a"))
143+
'((kind . "bench") (name . "b"))))
119144

120145
(it "'src/lib.rs' to the library target (custom-lib-target)"
121146
(expect
122147
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
123-
:to-equal "lib"))
148+
:to-equal '(kind . "lib")))
124149

125150
(it "'build.rs' to any target in the same workspace member (parent)"
126151
(expect
127152
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128-
:to-equal (cons "bin" "build-script-test")))
153+
:to-equal '((kind . "bin") (name . "build-script-test"))))
129154

130155
(it "'build.rs' to any target in the same workspace member (child)"
131156
(expect
132157
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133-
:to-equal (cons "lib" "lib-test")))
158+
:to-equal '((kind . "lib") (name . "lib-test"))))
159+
160+
(it "'src/main.rs' to the bin target with required-features (fea1)"
161+
(if (version< (cargo-version) "1.29")
162+
(signal 'buttercup-pending "requires cargo 1.29"))
163+
(expect
164+
(flycheck-rust-find-cargo-target (crate-with-features-file "src/main.rs"))
165+
:to-equal '((kind . "bin") (name . "main") (required-features . ("fea1")))))
166+
167+
(it "'example/example.rs' to the example target with required-features (fea1 fea2)"
168+
(if (version< (cargo-version) "1.29")
169+
(signal 'buttercup-pending "requires cargo 1.29"))
170+
(expect
171+
(flycheck-rust-find-cargo-target (crate-with-features-file "example/example.rs"))
172+
:to-equal '((kind . "example")
173+
(name . "example")
174+
(required-features . ("fea1" "fea2")))))
134175
)

0 commit comments

Comments
 (0)