Skip to content

Commit 24e0edd

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 24e0edd

File tree

5 files changed

+74
-25
lines changed

5 files changed

+74
-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

+42-17
Original file line numberDiff line numberDiff line change
@@ -38,97 +38,122 @@
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+
4752
(describe
4853
"`flycheck-rust-find-cargo-target' associates"
4954

5055
(it "'src/lib.rs' to the library target"
5156
(expect
52-
(car (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
57+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
5358
:to-equal "lib"))
5459

5560
(it "'src/a.rs' to the library target"
5661
(expect
57-
(car (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
62+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
5863
:to-equal "lib"))
5964

6065
(it "'src/main.rs' to the main binary target"
6166
(expect
6267
(flycheck-rust-find-cargo-target (crate-file "src/main.rs"))
63-
:to-equal '("bin" . "test-crate")))
68+
:to-equal '((kind . "bin") (name . "test-crate"))))
6469

6570
(it "'src/bin/a.rs' to the 'a' binary target"
6671
(expect
6772
(flycheck-rust-find-cargo-target (crate-file "src/bin/a.rs"))
68-
:to-equal '("bin" . "a")))
73+
:to-equal '((kind . "bin") (name . "a"))))
6974

7075
(it "'src/bin/b.rs' to the 'b' binary target"
7176
(expect
7277
(flycheck-rust-find-cargo-target (crate-file "src/bin/b.rs"))
73-
:to-equal '("bin" . "b")))
78+
:to-equal '((kind . "bin") (name . "b"))))
7479

7580
(it "'src/bin/support/mod.rs' to any binary target"
7681
(expect
7782
(flycheck-rust-find-cargo-target (crate-file "src/bin/support/mod.rs"))
78-
:to-equal-one-of '("bin". "a") '("bin". "b")))
83+
:to-equal-one-of
84+
'((kind . "bin") (name . "a"))
85+
'((kind . "bin") (name . "b"))))
7986

8087
(it "'tests/a.rs' to the 'a' test target"
8188
(expect
8289
(flycheck-rust-find-cargo-target (crate-file "tests/a.rs"))
83-
:to-equal '("test" . "a")))
90+
:to-equal '((kind . "test") (name . "a"))))
8491

8592
(it "'tests/support/mod.rs' to any test target"
8693
(expect
8794
(flycheck-rust-find-cargo-target (crate-file "tests/support/mod.rs"))
88-
:to-equal-one-of '("test". "a") '("test". "b")))
95+
:to-equal-one-of
96+
'((kind . "test") (name . "a"))
97+
'((kind . "test") (name . "b"))))
8998

9099
(it "'examples/a.rs' to the 'a' example target"
91100
(expect
92101
(flycheck-rust-find-cargo-target (crate-file "examples/a.rs"))
93-
:to-equal '("example" . "a")))
102+
:to-equal '((kind . "example") (name . "a"))))
94103

95104
(it "'examples/b.rs' to the 'b' example target"
96105
(expect
97106
(flycheck-rust-find-cargo-target (crate-file "examples/b.rs"))
98-
:to-equal '("example" . "b")))
107+
:to-equal '((kind . "example") (name . "b"))))
99108

100109
(it "'examples/support/mod.rs' to any example target"
101110
(expect
102111
(flycheck-rust-find-cargo-target (crate-file "examples/support/mod.rs"))
103-
:to-equal-one-of '("example" . "a") '("example" . "b")))
112+
:to-equal-one-of
113+
'((kind . "example") (name . "a"))
114+
'((kind . "example") (name . "b"))))
104115

105116
(it "'benches/a.rs' to the 'a' bench target"
106117
(expect
107118
(flycheck-rust-find-cargo-target (crate-file "benches/a.rs"))
108-
:to-equal '("bench" . "a")))
119+
:to-equal '((kind . "bench") (name . "a"))))
109120

110121
(it "'benches/b.rs' to the 'b' bench target"
111122
(expect
112123
(flycheck-rust-find-cargo-target (crate-file "benches/b.rs"))
113-
:to-equal '("bench" . "b")))
124+
:to-equal '((kind . "bench") (name . "b"))))
114125

115126
(it "'benches/support/mod.rs' to any bench target"
116127
(expect
117128
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
118-
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
129+
:to-equal-one-of
130+
'((kind . "bench") (name . "a"))
131+
'((kind . "bench") (name . "b"))))
119132

120133
(it "'src/lib.rs' to the library target (custom-lib-target)"
121134
(expect
122135
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
123-
:to-equal "lib"))
136+
:to-equal '(kind . "lib")))
124137

125138
(it "'build.rs' to any target in the same workspace member (parent)"
126139
(expect
127140
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128-
:to-equal (cons "bin" "build-script-test")))
141+
:to-equal '((kind . "bin") (name . "build-script-test"))))
129142

130143
(it "'build.rs' to any target in the same workspace member (child)"
131144
(expect
132145
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133-
:to-equal (cons "lib" "lib-test")))
146+
:to-equal '((kind . "lib") (name . "lib-test"))))
147+
148+
(it "'src/main.rs' to the bin target with required-features (fea1)"
149+
(expect
150+
(flycheck-rust-find-cargo-target (crate-with-features-file "src/main.rs"))
151+
:to-equal '((kind . "bin") (name . "main") (required-features . ("fea1")))))
152+
153+
(it "'example/example.rs' to the example target with required-features (fea1 fea2)"
154+
(expect
155+
(flycheck-rust-find-cargo-target (crate-with-features-file "example/example.rs"))
156+
:to-equal '((kind . "example")
157+
(name . "example")
158+
(required-features . ("fea1" "fea2")))))
134159
)

0 commit comments

Comments
 (0)