Skip to content

Commit 4b35914

Browse files
committed
target check_consistency: ensure target feature string makes some basic sense
1 parent 4e4c20d commit 4b35914

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

compiler/rustc_target/src/spec/tests/tests_impl.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::assert_matches::assert_matches;
22

3+
use rustc_data_structures::fx::FxHashSet;
4+
35
use super::super::*;
46

57
// Test target self-consistency and JSON encoding/decoding roundtrip.
@@ -170,6 +172,25 @@ impl Target {
170172
}
171173
_ => {}
172174
}
175+
176+
// Check that the given target-features string makes some basic sense.
177+
let mut features_enabled = FxHashSet::default();
178+
let mut features_disabled = FxHashSet::default();
179+
for feat in self.features.split(',') {
180+
if let Some(feat) = feat.strip_prefix("+") {
181+
features_enabled.insert(feat);
182+
if features_disabled.contains(feat) {
183+
panic!("target feature `{feat}` is both enabled and disabled");
184+
}
185+
} else if let Some(feat) = feat.strip_prefix("-") {
186+
features_disabled.insert(feat);
187+
if features_enabled.contains(feat) {
188+
panic!("target feature `{feat}` is both enabled and disabled");
189+
}
190+
} else {
191+
panic!("target feature `{feat}` is invalid, must start with `+` or `-`");
192+
}
193+
}
173194
}
174195

175196
// Add your target to the whitelist if it has `std` library

0 commit comments

Comments
 (0)