-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
rustdoc: Reify emission types #155679
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
base: main
Are you sure you want to change the base?
rustdoc: Reify emission types #155679
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ use rustc_session::{EarlyDiagCtxt, getopts}; | |
| use rustc_span::edition::Edition; | ||
| use rustc_span::{FileName, RemapPathScopeComponents}; | ||
| use rustc_target::spec::TargetTuple; | ||
| use smallvec::SmallVec; | ||
|
|
||
| use crate::core::new_dcx; | ||
| use crate::externalfiles::ExternalHtml; | ||
|
|
@@ -293,7 +294,7 @@ pub(crate) struct RenderOptions { | |
| /// Note: this field is duplicated in `Options` because it's useful to have | ||
| /// it in both places. | ||
| pub(crate) unstable_features: rustc_feature::UnstableFeatures, | ||
| pub(crate) emit: Vec<EmitType>, | ||
| pub(crate) emit: SmallVec<[EmitType; 2]>, | ||
| /// If `true`, HTML source pages will generate links for items to their definition. | ||
| pub(crate) generate_link_to_definition: bool, | ||
| /// Set of function-call locations to include as examples | ||
|
|
@@ -327,9 +328,22 @@ pub(crate) enum ModuleSorting { | |
| pub(crate) enum EmitType { | ||
| HtmlStaticFiles, | ||
| HtmlNonStaticFiles, | ||
| // not explicitly nameable by the user for now | ||
| JsonFiles, | ||
| DepInfo(Option<OutFileName>), | ||
| } | ||
|
|
||
| impl fmt::Display for EmitType { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(match self { | ||
| Self::HtmlStaticFiles => "html-static-files", | ||
| Self::HtmlNonStaticFiles => "html-non-static-files", | ||
| Self::JsonFiles => "json-files", | ||
| Self::DepInfo(_) => "dep-info", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for EmitType { | ||
| type Err = (); | ||
|
|
||
|
|
@@ -352,17 +366,11 @@ impl FromStr for EmitType { | |
| } | ||
|
|
||
| impl RenderOptions { | ||
| pub(crate) fn should_emit_crate(&self) -> bool { | ||
| self.emit.is_empty() || self.emit.contains(&EmitType::HtmlNonStaticFiles) | ||
| } | ||
|
|
||
| pub(crate) fn dep_info(&self) -> Option<Option<&OutFileName>> { | ||
| for emit in &self.emit { | ||
| if let EmitType::DepInfo(file) = emit { | ||
| return Some(file.as_ref()); | ||
| } | ||
| } | ||
| None | ||
| self.emit.iter().find_map(|emit| match emit { | ||
| EmitType::DepInfo(file) => Some(file.as_ref()), | ||
| _ => None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -469,26 +477,6 @@ impl Options { | |
|
|
||
| let should_test = matches.opt_present("test"); | ||
|
|
||
| let mut emit = FxIndexMap::<_, EmitType>::default(); | ||
| for list in matches.opt_strs("emit") { | ||
| if should_test { | ||
| dcx.fatal("the `--test` flag and the `--emit` flag are not supported together"); | ||
| } | ||
| for kind in list.split(',') { | ||
| match kind.parse() { | ||
| Ok(kind) => { | ||
| // De-duplicate emit types and the last wins. | ||
| // Only one instance for each type is allowed | ||
| // regardless the actual data it carries. | ||
| // This matches rustc's `--emit` behavior. | ||
| emit.insert(std::mem::discriminant(&kind), kind); | ||
| } | ||
| Err(()) => dcx.fatal(format!("unrecognized emission type: {kind}")), | ||
| } | ||
| } | ||
| } | ||
| let emit = emit.into_values().collect::<Vec<_>>(); | ||
|
|
||
| let show_coverage = matches.opt_present("show-coverage"); | ||
| let output_format_s = matches.opt_str("output-format"); | ||
| let output_format = match output_format_s { | ||
|
|
@@ -527,15 +515,55 @@ impl Options { | |
| } | ||
| } | ||
|
|
||
| if output_format == OutputFormat::Json { | ||
| if let Some(emit_flag) = emit.iter().find_map(|emit| match emit { | ||
| EmitType::HtmlStaticFiles => Some("html-static-files"), | ||
| EmitType::HtmlNonStaticFiles => Some("html-non-static-files"), | ||
| EmitType::DepInfo(_) => None, | ||
| }) { | ||
| dcx.fatal(format!( | ||
| "the `--emit={emit_flag}` flag is not supported with `--output-format=json`", | ||
| )); | ||
| let mut emit = FxIndexMap::default(); | ||
| for list in matches.opt_strs("emit") { | ||
| if should_test { | ||
| dcx.fatal("the `--test` flag and the `--emit` flag are not supported together"); | ||
| } | ||
| if let OutputFormat::Doctest = output_format { | ||
| dcx.fatal("the `--emit` flag is not supported with `--output-format=doctest`"); | ||
| } | ||
|
Comment on lines
+523
to
+525
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do like this PR I'll of course add a test for this. |
||
|
|
||
| for typ in list.split(',') { | ||
| let Ok(typ) = typ.parse::<EmitType>() else { | ||
| dcx.fatal(format!("unrecognized emission type: {typ}")) | ||
| }; | ||
|
|
||
| match typ { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
| EmitType::DepInfo(_) => match output_format { | ||
| OutputFormat::Json | OutputFormat::Html => {} | ||
| OutputFormat::Doctest => unreachable!(), | ||
| }, | ||
| EmitType::HtmlStaticFiles | EmitType::HtmlNonStaticFiles => match output_format | ||
| { | ||
| OutputFormat::Html => {} | ||
| OutputFormat::Json => dcx.fatal(format!( | ||
| "the `--emit={typ}` flag is not supported with `--output-format=json`", | ||
| )), | ||
| OutputFormat::Doctest => unreachable!(), | ||
| }, | ||
| EmitType::JsonFiles => unreachable!(), | ||
| } | ||
|
|
||
| // De-duplicate emit types and the last wins. | ||
| // Only one instance for each type is allowed | ||
| // regardless the actual data it carries. | ||
| // This matches rustc's `--emit` behavior. | ||
| emit.insert(std::mem::discriminant(&typ), typ); | ||
| } | ||
| } | ||
| let mut emit: SmallVec<[_; 2]> = emit.into_values().collect(); | ||
| // If `--emit` is absent we'll register default emission types depending on the requested | ||
| // output format. We can safely use `is_empty` for this since `--emit=` ("truly empty") | ||
| // will have already been rejected above. | ||
| if emit.is_empty() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, I could change the earlier |
||
| match output_format { | ||
| OutputFormat::Json => emit.push(EmitType::JsonFiles), | ||
| OutputFormat::Html => { | ||
| emit.push(EmitType::HtmlStaticFiles); | ||
| emit.push(EmitType::HtmlNonStaticFiles); | ||
| } | ||
| OutputFormat::Doctest => {} | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated and already brought up in the stabilization PR, I'm more and more leaning towards fully ditching
-w, --output-formatin favor of--emitbecause the current setup makes my head spin (the loose proposal to repurpose--outputand the one to add--printdoesn't help ^^).--emit=json-filesand--emit=doctestscould be wonderful. Of course, we probably want to make some emission types mutually exclusive then … which might not be in line with its "spirit" but oh wellView changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem obvious to me. I know there are several barriers that prevent rustdoc from emitting json and html at the same time:
clean::CratemutatesDocContext, so we can't just construct Clean twice.If either of those first two barriers were fixed, wouldn't
--emit=html-static-files,html-non-static-files,json-file=./target/doc-json/CRATENAME.jsonmake sense?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it would indeed make sense if it's feasible to fix these issues 👍.
Let me change my statement to the following then: "If we were to introduce
--emit=json-filesand ditch-wjson, we would want to make thehtml-*emission types incompatible withjson-filesfor the time being until we have figured out how to make them compatible implementation-wise".In any case, this topic isn't strictly related to this PR which is a mere internal refactoring plus a fix for an unstable feature (
-wdoctest) and shouldn't block it in the slightest :)