Skip to content

Commit

Permalink
Rename crate: to dep:
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Oct 25, 2020
1 parent 8ff130b commit f4ac82a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Available unstable (nightly-only) flags:
-Z timings -- Display concurrency information
-Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
-Z terminal-width -- Provide a terminal width to rustc for error truncation
-Z namespaced-features -- Allow features with `crate:` prefix
-Z namespaced-features -- Allow features with `dep:` prefix
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
);
Expand Down
24 changes: 12 additions & 12 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub fn resolve_features<'b>(
}

// This is a special case for command-line `--features
// crate_name/feat_name` where `crate_name` does not exist. All other
// dep_name/feat_name` where `dep_name` does not exist. All other
// validation is done either in `build_requirements` or
// `build_feature_map`.
for dep_name in reqs.deps.keys() {
Expand Down Expand Up @@ -374,9 +374,9 @@ fn build_requirements<'a, 'b: 'a>(
} else {
for &f in opts.features.features.iter() {
let fv = FeatureValue::new(f);
if fv.has_crate_prefix() {
if fv.has_dep_prefix() {
return Err(ActivateError::Fatal(anyhow::format_err!(
"feature value `{}` is not allowed to use explicit `crate:` syntax",
"feature value `{}` is not allowed to use explicit `dep:` syntax",
fv
)));
}
Expand Down Expand Up @@ -438,16 +438,16 @@ impl Requirements<'_> {
self.features
}

fn require_crate_feature(
fn require_dep_feature(
&mut self,
package: InternedString,
feat: InternedString,
crate_prefix: bool,
dep_prefix: bool,
) -> Result<(), RequirementError> {
// If `package` is indeed an optional dependency then we activate the
// feature named `package`, but otherwise if `package` is a required
// dependency then there's no feature associated with it.
if !crate_prefix
if !dep_prefix
&& self
.summary
.dependencies()
Expand Down Expand Up @@ -489,12 +489,12 @@ impl Requirements<'_> {
fn require_value(&mut self, fv: &FeatureValue) -> Result<(), RequirementError> {
match fv {
FeatureValue::Feature(feat) => self.require_feature(*feat)?,
FeatureValue::Crate { dep_name } => self.require_dependency(*dep_name),
FeatureValue::CrateFeature {
FeatureValue::Dep { dep_name } => self.require_dependency(*dep_name),
FeatureValue::DepFeature {
dep_name,
dep_feature,
crate_prefix,
} => self.require_crate_feature(*dep_name, *dep_feature, *crate_prefix)?,
dep_prefix,
} => self.require_dep_feature(*dep_name, *dep_feature, *dep_prefix)?,
};
Ok(())
}
Expand Down Expand Up @@ -526,7 +526,7 @@ impl RequirementError {
match parent {
None => ActivateError::Fatal(anyhow::format_err!(
"Package `{}` does not have feature `{}`. It has an optional dependency \
with that name, but that dependency uses the \"crate:\" \
with that name, but that dependency uses the \"dep:\" \
syntax in the features table, so it does not have an implicit feature with that name.",
summary.package_id(),
feat
Expand Down Expand Up @@ -559,7 +559,7 @@ impl RequirementError {
dep_name
)),
// This code path currently isn't used, since `foo/bar`
// and `crate:` syntax is not allowed in a dependency.
// and `dep:` syntax is not allowed in a dependency.
Some(p) => ActivateError::Conflict(
p,
ConflictReason::MissingFeatures(dep_name.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub(super) fn activation_error(
msg.push_str("` does not have these features.\n");
msg.push_str(
" It has an optional dependency with that name, \
but but that dependency uses the \"crate:\" \
but but that dependency uses the \"dep:\" \
syntax in the features table, so it does not have an \
implicit feature with that name.\n",
);
Expand Down
14 changes: 7 additions & 7 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
// For example, consider we've already processed our dependencies,
// and another package comes along and enables one of our optional
// dependencies, it will do so immediately in the
// `FeatureValue::CrateFeature` branch, and then immediately
// `FeatureValue::DepFeature` branch, and then immediately
// recurse into that optional dependency. This also holds true for
// features that enable other features.
return Ok(());
Expand Down Expand Up @@ -443,7 +443,7 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
FeatureValue::Feature(f) => {
self.activate_rec(pkg_id, *f, for_host)?;
}
FeatureValue::Crate { dep_name } => {
FeatureValue::Dep { dep_name } => {
// Mark this dependency as activated.
self.activated_dependencies
.entry((pkg_id, self.opts.decouple_host_deps && for_host))
Expand All @@ -460,10 +460,10 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
}
}
}
FeatureValue::CrateFeature {
FeatureValue::DepFeature {
dep_name,
dep_feature,
crate_prefix,
dep_prefix,
} => {
// Activate a feature within a dependency.
for (dep_pkg_id, deps) in self.deps(pkg_id, for_host) {
Expand All @@ -472,12 +472,12 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
continue;
}
if dep.is_optional() {
// Activate the crate on self.
let fv = FeatureValue::Crate {
// Activate the dependency on self.
let fv = FeatureValue::Dep {
dep_name: *dep_name,
};
self.activate_fv(pkg_id, &fv, for_host)?;
if !crate_prefix {
if !dep_prefix {
// To retain compatibility with old behavior,
// this also enables a feature of the same
// name.
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ pub enum ConflictReason {
RequiredDependencyAsFeature(InternedString),

/// A dependency listed a feature for an optional dependency, but that
/// optional dependency is "hidden" using namespaced `crate:` syntax.
/// optional dependency is "hidden" using namespaced `dep:` syntax.
NonImplicitDependencyAsFeature(InternedString),

// TODO: needs more info for `activation_error`
Expand Down
77 changes: 41 additions & 36 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Summary {
if !namespaced_features {
if self.inner.has_namespaced_features {
bail!(
"namespaced features with the `crate:` prefix are only allowed on \
"namespaced features with the `dep:` prefix are only allowed on \
the nightly channel and requires the `-Z namespaced-features` flag on the command-line"
);
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl Hash for Summary {
/// and creates FeatureValues for each feature.
///
/// The returned `bool` indicates whether or not the `[features]` table
/// included a `crate:` prefixed namespaced feature (used for gating on
/// included a `dep:` prefixed namespaced feature (used for gating on
/// nightly).
fn build_feature_map(
features: &BTreeMap<InternedString, Vec<InternedString>>,
Expand All @@ -183,18 +183,18 @@ fn build_feature_map(
(*feature, fvs)
})
.collect();
let has_namespaced_features = map.values().flatten().any(|fv| fv.has_crate_prefix());
let has_namespaced_features = map.values().flatten().any(|fv| fv.has_dep_prefix());

// Add implicit features for optional dependencies if they weren't
// explicitly listed anywhere.
let explicitly_listed: HashSet<_> = map
.values()
.flatten()
.filter_map(|fv| match fv {
Crate { dep_name }
| CrateFeature {
Dep { dep_name }
| DepFeature {
dep_name,
crate_prefix: true,
dep_prefix: true,
..
} => Some(*dep_name),
_ => None,
Expand All @@ -209,25 +209,25 @@ fn build_feature_map(
{
continue;
}
let fv = Crate {
let fv = Dep {
dep_name: dep_name_in_toml,
};
map.insert(dep_name_in_toml, vec![fv]);
}

// Validate features are listed properly.
for (feature, fvs) in &map {
if feature.starts_with("crate:") {
if feature.starts_with("dep:") {
bail!(
"feature named `{}` is not allowed to start with `crate:`",
"feature named `{}` is not allowed to start with `dep:`",
feature
);
}
for fv in fvs {
// Find data for the referenced dependency...
let dep_data = {
match fv {
Feature(dep_name) | Crate { dep_name, .. } | CrateFeature { dep_name, .. } => {
Feature(dep_name) | Dep { dep_name, .. } | DepFeature { dep_name, .. } => {
dep_map.get(dep_name)
}
}
Expand All @@ -253,7 +253,7 @@ fn build_feature_map(
bail!(
"feature `{}` includes `{}`, but `{}` is an \
optional dependency without an implicit feature\n\
Use `crate:{}` to enable the dependency.",
Use `dep:{}` to enable the dependency.",
feature,
fv,
f,
Expand All @@ -268,7 +268,7 @@ fn build_feature_map(
}
}
}
Crate { dep_name } => {
Dep { dep_name } => {
if !is_any_dep {
bail!(
"feature `{}` includes `{}`, but `{}` is not listed as a dependency",
Expand All @@ -288,7 +288,7 @@ fn build_feature_map(
);
}
}
CrateFeature { dep_name, .. } => {
DepFeature { dep_name, .. } => {
// Validation of the feature name will be performed in the resolver.
if !is_any_dep {
bail!(
Expand All @@ -308,7 +308,7 @@ fn build_feature_map(
.values()
.flatten()
.filter_map(|fv| match fv {
Crate { dep_name } | CrateFeature { dep_name, .. } => Some(dep_name),
Dep { dep_name } | DepFeature { dep_name, .. } => Some(dep_name),
_ => None,
})
.collect();
Expand All @@ -318,7 +318,7 @@ fn build_feature_map(
{
bail!(
"optional dependency `{}` is not included in any feature\n\
Make sure that `crate:{}` is included in one of features in the [features] table.",
Make sure that `dep:{}` is included in one of features in the [features] table.",
dep.name_in_toml(),
dep.name_in_toml(),
);
Expand All @@ -332,15 +332,15 @@ fn build_feature_map(
pub enum FeatureValue {
/// A feature enabling another feature.
Feature(InternedString),
/// A feature enabling a dependency with `crate:dep_name` syntax.
Crate { dep_name: InternedString },
/// A feature enabling a dependency with `dep:dep_name` syntax.
Dep { dep_name: InternedString },
/// A feature enabling a feature on a dependency with `crate_name/feat_name` syntax.
CrateFeature {
DepFeature {
dep_name: InternedString,
dep_feature: InternedString,
/// If this is true, then the feature used the `crate:` prefix, which
/// If this is true, then the feature used the `dep:` prefix, which
/// prevents enabling the feature named `dep_name`.
crate_prefix: bool,
dep_prefix: bool,
},
}

Expand All @@ -350,27 +350,32 @@ impl FeatureValue {
Some(pos) => {
let (dep, dep_feat) = feature.split_at(pos);
let dep_feat = &dep_feat[1..];
let (dep, crate_prefix) = if let Some(dep) = dep.strip_prefix("crate:") {
let (dep, dep_prefix) = if let Some(dep) = dep.strip_prefix("dep:") {
(dep, true)
} else {
(dep, false)
};
FeatureValue::CrateFeature {
FeatureValue::DepFeature {
dep_name: InternedString::new(dep),
dep_feature: InternedString::new(dep_feat),
crate_prefix,
dep_prefix,
}
}
None => {
if let Some(dep_name) = feature.strip_prefix("dep:") {
FeatureValue::Dep {
dep_name: InternedString::new(dep_name),
}
} else {
FeatureValue::Feature(feature)
}
}
None if feature.starts_with("crate:") => FeatureValue::Crate {
dep_name: InternedString::new(&feature[6..]),
},
None => FeatureValue::Feature(feature),
}
}

/// Returns `true` if this feature explicitly used `crate:` syntax.
pub fn has_crate_prefix(&self) -> bool {
matches!(self, FeatureValue::Crate{..} | FeatureValue::CrateFeature{crate_prefix:true, ..})
/// Returns `true` if this feature explicitly used `dep:` syntax.
pub fn has_dep_prefix(&self) -> bool {
matches!(self, FeatureValue::Dep{..} | FeatureValue::DepFeature{dep_prefix:true, ..})
}
}

Expand All @@ -379,16 +384,16 @@ impl fmt::Display for FeatureValue {
use self::FeatureValue::*;
match self {
Feature(feat) => write!(f, "{}", feat),
Crate { dep_name } => write!(f, "crate:{}", dep_name),
CrateFeature {
Dep { dep_name } => write!(f, "dep:{}", dep_name),
DepFeature {
dep_name,
dep_feature,
crate_prefix: true,
} => write!(f, "crate:{}/{}", dep_name, dep_feature),
CrateFeature {
dep_prefix: true,
} => write!(f, "dep:{}/{}", dep_name, dep_feature),
DepFeature {
dep_name,
dep_feature,
crate_prefix: false,
dep_prefix: false,
} => write!(f, "{}/{}", dep_name, dep_feature),
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,22 +1071,22 @@ fn validate_required_features(
))?;
}
}
FeatureValue::Crate { .. }
| FeatureValue::CrateFeature {
crate_prefix: true, ..
FeatureValue::Dep { .. }
| FeatureValue::DepFeature {
dep_prefix: true, ..
} => {
anyhow::bail!(
"invalid feature `{}` in required-features of target `{}`: \
`crate:` prefixed feature values are not allowed in required-features",
`dep:` prefixed feature values are not allowed in required-features",
fv,
target_name
);
}
// Handling of dependent_crate/dependent_crate_feature syntax
FeatureValue::CrateFeature {
FeatureValue::DepFeature {
dep_name,
dep_feature,
crate_prefix: false,
dep_prefix: false,
} => {
match resolve
.deps(summary.package_id())
Expand Down
Loading

0 comments on commit f4ac82a

Please sign in to comment.