Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Output target feature information #139393

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ impl<'tcx> JsonRenderer<'tcx> {
Ok(())
})
}

pub(crate) fn target(&self) -> types::Target {
let sess = self.tcx.sess;

let globally_enabled_features: FxHashMap<&str, ()> =
sess.target_features.iter().map(|sym| (sym.as_str(), ())).collect();

types::Target {
target_features: sess
.target
.rust_target_features()
.into_iter()
.copied()
.map(|(name, stability, implied)| types::TargetFeature {
name: name.into(),
stable: matches!(stability, rustc_target::target_features::Stability::Stable),
implies_features: implied.into_iter().copied().map(String::from).collect(),
globally_enabled: globally_enabled_features.contains_key(name),
})
.collect(),
}
}
}

impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
Expand Down Expand Up @@ -288,6 +310,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
)
})
.collect(),
target: self.target(),
format_version: types::FORMAT_VERSION,
};
if let Some(ref out_dir) = self.out_dir {
Expand Down
24 changes: 24 additions & 0 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,35 @@ pub struct Crate {
pub paths: HashMap<Id, ItemSummary>,
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
pub external_crates: HashMap<u32, ExternalCrate>,
/// Information about the target for which this documentation was generated
pub target: Target,
/// A single version number to be used in the future when making backwards incompatible changes
/// to the JSON output.
pub format_version: u32,
}

/// Information about a target
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
/// A list of `#[target_feature]` which exist for this target, along with their status in this
/// compiler session
pub target_features: Vec<TargetFeature>,
}

/// Information about a `#[target_feature]`
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TargetFeature {
/// The name of this feature
pub name: String,
/// Other feature(s) which are implied by this feature
pub implies_features: Vec<String>,
/// Whether this feature is stable
pub stable: bool,
/// Whether this feature is globally enabled for this target, for example, due to the
/// target's ABI requiring this feature, or due to compiler flags which enable it everywhere
pub globally_enabled: bool,
}

/// Metadata of a crate, either the same crate on which `rustdoc` was invoked, or its dependency.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ExternalCrate {
Expand Down
4 changes: 4 additions & 0 deletions src/tools/jsondoclint/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn errors_on_missing_links() {
)]),
paths: FxHashMap::default(),
external_crates: FxHashMap::default(),
target: rustdoc_json_types::Target { target_features: vec![] },
format_version: rustdoc_json_types::FORMAT_VERSION,
};

Expand Down Expand Up @@ -112,6 +113,7 @@ fn errors_on_local_in_paths_and_not_index() {
},
)]),
external_crates: FxHashMap::default(),
target: rustdoc_json_types::Target { target_features: vec![] },
format_version: rustdoc_json_types::FORMAT_VERSION,
};

Expand Down Expand Up @@ -216,6 +218,7 @@ fn errors_on_missing_path() {
ItemSummary { crate_id: 0, path: vec!["foo".to_owned()], kind: ItemKind::Module },
)]),
external_crates: FxHashMap::default(),
target: rustdoc_json_types::Target { target_features: vec![] },
format_version: rustdoc_json_types::FORMAT_VERSION,
};

Expand Down Expand Up @@ -259,6 +262,7 @@ fn checks_local_crate_id_is_correct() {
)]),
paths: FxHashMap::default(),
external_crates: FxHashMap::default(),
target: rustdoc_json_types::Target { target_features: vec![] },
format_version: FORMAT_VERSION,
};
check(&krate, &[]);
Expand Down
Loading