Skip to content

Commit 9835622

Browse files
committed
Convert valid feature name warning to an error.
1 parent 768339b commit 9835622

File tree

6 files changed

+102
-105
lines changed

6 files changed

+102
-105
lines changed

crates/resolver-tests/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ pub fn resolve_with_config_raw(
179179
used: HashSet::new(),
180180
};
181181
let summary = Summary::new(
182-
config,
183182
pkg_id("root"),
184183
deps,
185184
&BTreeMap::new(),
@@ -581,7 +580,6 @@ pub fn pkg_dep<T: ToPkgId>(name: T, dep: Vec<Dependency>) -> Summary {
581580
None
582581
};
583582
Summary::new(
584-
&Config::default().unwrap(),
585583
name.to_pkgid(),
586584
dep,
587585
&BTreeMap::new(),
@@ -610,7 +608,6 @@ pub fn pkg_loc(name: &str, loc: &str) -> Summary {
610608
None
611609
};
612610
Summary::new(
613-
&Config::default().unwrap(),
614611
pkg_id_loc(name, loc),
615612
Vec::new(),
616613
&BTreeMap::new(),
@@ -625,7 +622,6 @@ pub fn remove_dep(sum: &Summary, ind: usize) -> Summary {
625622
deps.remove(ind);
626623
// note: more things will need to be copied over in the future, but it works for now.
627624
Summary::new(
628-
&Config::default().unwrap(),
629625
sum.package_id(),
630626
deps,
631627
&BTreeMap::new(),

src/cargo/core/resolver/version_prefs.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ impl VersionPreferences {
8181
mod test {
8282
use super::*;
8383
use crate::core::SourceId;
84-
use crate::util::Config;
8584
use std::collections::BTreeMap;
8685

8786
fn pkgid(name: &str, version: &str) -> PackageId {
@@ -98,10 +97,8 @@ mod test {
9897

9998
fn summ(name: &str, version: &str) -> Summary {
10099
let pkg_id = pkgid(name, version);
101-
let config = Config::default().unwrap();
102100
let features = BTreeMap::new();
103101
Summary::new(
104-
&config,
105102
pkg_id,
106103
Vec::new(),
107104
&features,

src/cargo/core/summary.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::core::{Dependency, PackageId, SourceId};
22
use crate::util::interning::InternedString;
3-
use crate::util::{CargoResult, Config};
3+
use crate::util::CargoResult;
44
use anyhow::bail;
55
use semver::Version;
66
use std::collections::{BTreeMap, HashMap, HashSet};
@@ -30,7 +30,6 @@ struct Inner {
3030

3131
impl Summary {
3232
pub fn new(
33-
config: &Config,
3433
pkg_id: PackageId,
3534
dependencies: Vec<Dependency>,
3635
features: &BTreeMap<InternedString, Vec<InternedString>>,
@@ -49,7 +48,7 @@ impl Summary {
4948
)
5049
}
5150
}
52-
let feature_map = build_feature_map(config, pkg_id, features, &dependencies)?;
51+
let feature_map = build_feature_map(pkg_id, features, &dependencies)?;
5352
Ok(Summary {
5453
inner: Rc::new(Inner {
5554
package_id: pkg_id,
@@ -140,7 +139,6 @@ impl Hash for Summary {
140139
/// Checks features for errors, bailing out a CargoResult:Err if invalid,
141140
/// and creates FeatureValues for each feature.
142141
fn build_feature_map(
143-
config: &Config,
144142
pkg_id: PackageId,
145143
features: &BTreeMap<InternedString, Vec<InternedString>>,
146144
dependencies: &[Dependency],
@@ -204,7 +202,7 @@ fn build_feature_map(
204202
feature
205203
);
206204
}
207-
validate_feature_name(config, pkg_id, feature)?;
205+
validate_feature_name(pkg_id, feature)?;
208206
for fv in fvs {
209207
// Find data for the referenced dependency...
210208
let dep_data = {
@@ -431,33 +429,63 @@ impl fmt::Display for FeatureValue {
431429

432430
pub type FeatureMap = BTreeMap<InternedString, Vec<FeatureValue>>;
433431

434-
fn validate_feature_name(config: &Config, pkg_id: PackageId, name: &str) -> CargoResult<()> {
432+
fn validate_feature_name(pkg_id: PackageId, name: &str) -> CargoResult<()> {
435433
let mut chars = name.chars();
436-
const FUTURE: &str = "This was previously accepted but is being phased out; \
437-
it will become a hard error in a future release.\n\
438-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, \
439-
and please leave a comment if this will be a problem for your project.";
440434
if let Some(ch) = chars.next() {
441435
if !(unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' || ch.is_digit(10)) {
442-
config.shell().warn(&format!(
436+
bail!(
443437
"invalid character `{}` in feature `{}` in package {}, \
444438
the first character must be a Unicode XID start character or digit \
445-
(most letters or `_` or `0` to `9`)\n\
446-
{}",
447-
ch, name, pkg_id, FUTURE
448-
))?;
439+
(most letters or `_` or `0` to `9`)",
440+
ch,
441+
name,
442+
pkg_id
443+
);
449444
}
450445
}
451446
for ch in chars {
452447
if !(unicode_xid::UnicodeXID::is_xid_continue(ch) || ch == '-' || ch == '+' || ch == '.') {
453-
config.shell().warn(&format!(
448+
bail!(
454449
"invalid character `{}` in feature `{}` in package {}, \
455450
characters must be Unicode XID characters, `+`, or `.` \
456-
(numbers, `+`, `-`, `_`, `.`, or most letters)\n\
457-
{}",
458-
ch, name, pkg_id, FUTURE
459-
))?;
451+
(numbers, `+`, `-`, `_`, `.`, or most letters)",
452+
ch,
453+
name,
454+
pkg_id
455+
);
460456
}
461457
}
462458
Ok(())
463459
}
460+
461+
#[cfg(test)]
462+
mod tests {
463+
use super::*;
464+
use crate::sources::CRATES_IO_INDEX;
465+
use crate::util::into_url::IntoUrl;
466+
467+
use crate::core::SourceId;
468+
469+
#[test]
470+
fn valid_feature_names() {
471+
let loc = CRATES_IO_INDEX.into_url().unwrap();
472+
let source_id = SourceId::for_registry(&loc).unwrap();
473+
let pkg_id = PackageId::new("foo", "1.0.0", source_id).unwrap();
474+
475+
assert!(validate_feature_name(pkg_id, "c++17").is_ok());
476+
assert!(validate_feature_name(pkg_id, "128bit").is_ok());
477+
assert!(validate_feature_name(pkg_id, "_foo").is_ok());
478+
assert!(validate_feature_name(pkg_id, "feat-name").is_ok());
479+
assert!(validate_feature_name(pkg_id, "feat_name").is_ok());
480+
assert!(validate_feature_name(pkg_id, "foo.bar").is_ok());
481+
482+
assert!(validate_feature_name(pkg_id, "+foo").is_err());
483+
assert!(validate_feature_name(pkg_id, "-foo").is_err());
484+
assert!(validate_feature_name(pkg_id, ".foo").is_err());
485+
assert!(validate_feature_name(pkg_id, "foo:bar").is_err());
486+
assert!(validate_feature_name(pkg_id, "foo?").is_err());
487+
assert!(validate_feature_name(pkg_id, "?foo").is_err());
488+
assert!(validate_feature_name(pkg_id, "ⒶⒷⒸ").is_err());
489+
assert!(validate_feature_name(pkg_id, "a¼").is_err());
490+
}
491+
}

src/cargo/sources/registry/index.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ impl<'cfg> RegistryIndex<'cfg> {
408408
'a: 'b,
409409
{
410410
let source_id = self.source_id;
411-
let config = self.config;
412411

413412
// First up parse what summaries we have available.
414413
let name = InternedString::new(name);
@@ -425,15 +424,13 @@ impl<'cfg> RegistryIndex<'cfg> {
425424
.versions
426425
.iter_mut()
427426
.filter_map(move |(k, v)| if req.matches(k) { Some(v) } else { None })
428-
.filter_map(
429-
move |maybe| match maybe.parse(config, raw_data, source_id) {
430-
Ok(summary) => Some(summary),
431-
Err(e) => {
432-
info!("failed to parse `{}` registry package: {}", name, e);
433-
None
434-
}
435-
},
436-
)
427+
.filter_map(move |maybe| match maybe.parse(raw_data, source_id) {
428+
Ok(summary) => Some(summary),
429+
Err(e) => {
430+
info!("failed to parse `{}` registry package: {}", name, e);
431+
None
432+
}
433+
})
437434
.filter(move |is| {
438435
if is.v > INDEX_V_MAX {
439436
debug!(
@@ -714,7 +711,7 @@ impl Summaries {
714711
// allow future cargo implementations to break the
715712
// interpretation of each line here and older cargo will simply
716713
// ignore the new lines.
717-
let summary = match IndexSummary::parse(config, line, source_id) {
714+
let summary = match IndexSummary::parse(line, source_id) {
718715
Ok(summary) => summary,
719716
Err(e) => {
720717
// This should only happen when there is an index
@@ -863,17 +860,12 @@ impl MaybeIndexSummary {
863860
/// Does nothing if this is already `Parsed`, and otherwise the `raw_data`
864861
/// passed in is sliced with the bounds in `Unparsed` and then actually
865862
/// parsed.
866-
fn parse(
867-
&mut self,
868-
config: &Config,
869-
raw_data: &[u8],
870-
source_id: SourceId,
871-
) -> CargoResult<&IndexSummary> {
863+
fn parse(&mut self, raw_data: &[u8], source_id: SourceId) -> CargoResult<&IndexSummary> {
872864
let (start, end) = match self {
873865
MaybeIndexSummary::Unparsed { start, end } => (*start, *end),
874866
MaybeIndexSummary::Parsed(summary) => return Ok(summary),
875867
};
876-
let summary = IndexSummary::parse(config, &raw_data[start..end], source_id)?;
868+
let summary = IndexSummary::parse(&raw_data[start..end], source_id)?;
877869
*self = MaybeIndexSummary::Parsed(summary);
878870
match self {
879871
MaybeIndexSummary::Unparsed { .. } => unreachable!(),
@@ -894,7 +886,7 @@ impl IndexSummary {
894886
///
895887
/// The `line` provided is expected to be valid JSON. It is supposed to be
896888
/// a [`IndexPackage`].
897-
fn parse(config: &Config, line: &[u8], source_id: SourceId) -> CargoResult<IndexSummary> {
889+
fn parse(line: &[u8], source_id: SourceId) -> CargoResult<IndexSummary> {
898890
// ****CAUTION**** Please be extremely careful with returning errors
899891
// from this function. Entries that error are not included in the
900892
// index cache, and can cause cargo to get confused when switching
@@ -925,7 +917,7 @@ impl IndexSummary {
925917
features.entry(name).or_default().extend(values);
926918
}
927919
}
928-
let mut summary = Summary::new(config, pkgid, deps, &features, links, rust_version)?;
920+
let mut summary = Summary::new(pkgid, deps, &features, links, rust_version)?;
929921
summary.set_checksum(cksum);
930922
Ok(IndexSummary {
931923
summary,

src/cargo/util/toml/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,6 @@ impl TomlManifest {
24322432
let empty_features = BTreeMap::new();
24332433

24342434
let summary = Summary::new(
2435-
config,
24362435
pkgid,
24372436
deps,
24382437
me.features.as_ref().unwrap_or(&empty_features),

tests/testsuite/features.rs

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,8 +1937,8 @@ fn nonexistent_required_features() {
19371937
}
19381938

19391939
#[cargo_test]
1940-
fn invalid_feature_names_warning() {
1941-
// Warnings for more restricted feature syntax.
1940+
fn invalid_feature_names_error() {
1941+
// Errors for more restricted feature syntax.
19421942
let p = project()
19431943
.file(
19441944
"Cargo.toml",
@@ -1948,72 +1948,57 @@ fn invalid_feature_names_warning() {
19481948
version = "0.1.0"
19491949
19501950
[features]
1951-
# Some valid, but unusual names, shouldn't warn.
1952-
"c++17" = []
1953-
"128bit" = []
1954-
"_foo" = []
1955-
"feat-name" = []
1956-
"feat_name" = []
1957-
"foo.bar" = []
1958-
1959-
# Invalid names.
1951+
# Invalid start character.
19601952
"+foo" = []
1961-
"-foo" = []
1962-
".foo" = []
1963-
"foo:bar" = []
1964-
"foo?" = []
1965-
"?foo" = []
1966-
"ⒶⒷⒸ" = []
1967-
"a¼" = []
19681953
"#,
19691954
)
19701955
.file("src/lib.rs", "")
19711956
.build();
19721957

1973-
// Unfortunately the warnings are duplicated due to the Summary being
1974-
// loaded twice (once in the Workspace, and once in PackageRegistry) and
1975-
// Cargo does not have a de-duplication system. This should probably be
1976-
// OK, since I'm not expecting this to affect anyone.
19771958
p.cargo("check")
1978-
.with_stderr("\
1979-
[WARNING] invalid character `+` in feature `+foo` in package foo v0.1.0 ([ROOT]/foo), the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)
1980-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1981-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1982-
[WARNING] invalid character `-` in feature `-foo` in package foo v0.1.0 ([ROOT]/foo), the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)
1983-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1984-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1985-
[WARNING] invalid character `.` in feature `.foo` in package foo v0.1.0 ([ROOT]/foo), the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)
1986-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1987-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1988-
[WARNING] invalid character `?` in feature `?foo` in package foo v0.1.0 ([ROOT]/foo), the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)
1989-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1990-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1991-
[WARNING] invalid character `¼` in feature `a¼` in package foo v0.1.0 ([ROOT]/foo), characters must be Unicode XID characters, `+`, or `.` (numbers, `+`, `-`, `_`, `.`, or most letters)
1992-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1993-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1994-
[WARNING] invalid character `:` in feature `foo:bar` in package foo v0.1.0 ([ROOT]/foo), characters must be Unicode XID characters, `+`, or `.` (numbers, `+`, `-`, `_`, `.`, or most letters)
1995-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1996-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
1997-
[WARNING] invalid character `?` in feature `foo?` in package foo v0.1.0 ([ROOT]/foo), characters must be Unicode XID characters, `+`, or `.` (numbers, `+`, `-`, `_`, `.`, or most letters)
1998-
This was previously accepted but is being phased out; it will become a hard error in a future release.
1999-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
2000-
[WARNING] invalid character `Ⓐ` in feature `ⒶⒷⒸ` in package foo v0.1.0 ([ROOT]/foo), the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)
2001-
This was previously accepted but is being phased out; it will become a hard error in a future release.
2002-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
2003-
[WARNING] invalid character `Ⓑ` in feature `ⒶⒷⒸ` in package foo v0.1.0 ([ROOT]/foo), characters must be Unicode XID characters, `+`, or `.` (numbers, `+`, `-`, `_`, `.`, or most letters)
2004-
This was previously accepted but is being phased out; it will become a hard error in a future release.
2005-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
2006-
[WARNING] invalid character `Ⓒ` in feature `ⒶⒷⒸ` in package foo v0.1.0 ([ROOT]/foo), characters must be Unicode XID characters, `+`, or `.` (numbers, `+`, `-`, `_`, `.`, or most letters)
2007-
This was previously accepted but is being phased out; it will become a hard error in a future release.
2008-
For more information, see issue #8813 <https://github.com/rust-lang/cargo/issues/8813>, and please leave a comment if this will be a problem for your project.
2009-
[CHECKING] foo v0.1.0 [..]
2010-
[FINISHED] [..]
2011-
")
1959+
.with_status(101)
1960+
.with_stderr(
1961+
"\
1962+
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`
1963+
1964+
Caused by:
1965+
invalid character `+` in feature `+foo` in package foo v0.1.0 ([ROOT]/foo), \
1966+
the first character must be a Unicode XID start character or digit \
1967+
(most letters or `_` or `0` to `9`)
1968+
",
1969+
)
1970+
.run();
1971+
1972+
p.change_file(
1973+
"Cargo.toml",
1974+
r#"
1975+
[package]
1976+
name = "foo"
1977+
version = "0.1.0"
1978+
1979+
[features]
1980+
# Invalid continue character.
1981+
"a&b" = []
1982+
"#,
1983+
);
1984+
1985+
p.cargo("check")
1986+
.with_status(101)
1987+
.with_stderr(
1988+
"\
1989+
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`
1990+
1991+
Caused by:
1992+
invalid character `&` in feature `a&b` in package foo v0.1.0 ([ROOT]/foo), \
1993+
characters must be Unicode XID characters, `+`, or `.` \
1994+
(numbers, `+`, `-`, `_`, `.`, or most letters)
1995+
",
1996+
)
20121997
.run();
20131998
}
20141999

20152000
#[cargo_test]
2016-
fn invalid_feature_names_error() {
2001+
fn invalid_feature_name_slash_error() {
20172002
// Errors for more restricted feature syntax.
20182003
let p = project()
20192004
.file(

0 commit comments

Comments
 (0)