diff options
| author | Jeremy Fitzhardinge <jeremy@goop.org> | 2020-05-25 16:21:25 -0700 |
|---|---|---|
| committer | Jeremy Fitzhardinge <jeremy@goop.org> | 2021-02-07 14:54:20 -0800 |
| commit | 82ccb6582a76d72fb5abaf7195dc7a653f1aa54b (patch) | |
| tree | 3b576e8c494f7aab329b398836948bfb3747f754 /compiler/rustc_errors | |
| parent | 9778068cbc1e06cc3685422323ff38a2f397de26 (diff) | |
| download | rust-82ccb6582a76d72fb5abaf7195dc7a653f1aa54b.tar.gz rust-82ccb6582a76d72fb5abaf7195dc7a653f1aa54b.zip | |
Add `--extern-loc` to augment unused crate dependency diagnostics
This allows a build system to indicate a location in its own dependency
specification files (eg Cargo's `Cargo.toml`) which can be reported
along side any unused crate dependency.
This supports several types of location:
- 'json' - provide some json-structured data, which is included in the json diagnostics
in a `tool_metadata` field
- 'raw' - emit the provided string into the output. This also appears as a json string in
`tool_metadata`.
If no `--extern-location` is explicitly provided then a default json entry of the form
`"tool_metadata":{"name":<cratename>,"path":<cratepath>}` is emitted.
Diffstat (limited to 'compiler/rustc_errors')
| -rw-r--r-- | compiler/rustc_errors/src/diagnostic.rs | 24 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/lib.rs | 35 |
3 files changed, 64 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index e61476bf23e..ef4a45cab41 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -4,7 +4,9 @@ use crate::Level; use crate::Substitution; use crate::SubstitutionPart; use crate::SuggestionStyle; +use crate::ToolMetadata; use rustc_lint_defs::Applicability; +use rustc_serialize::json::Json; use rustc_span::{MultiSpan, Span, DUMMY_SP}; use std::fmt; @@ -303,6 +305,7 @@ impl Diagnostic { msg: msg.to_owned(), style: SuggestionStyle::ShowCode, applicability, + tool_metadata: Default::default(), }); self } @@ -328,6 +331,7 @@ impl Diagnostic { msg: msg.to_owned(), style: SuggestionStyle::ShowCode, applicability, + tool_metadata: Default::default(), }); self } @@ -354,6 +358,7 @@ impl Diagnostic { msg: msg.to_owned(), style: SuggestionStyle::CompletelyHidden, applicability, + tool_metadata: Default::default(), }); self } @@ -408,6 +413,7 @@ impl Diagnostic { msg: msg.to_owned(), style, applicability, + tool_metadata: Default::default(), }); self } @@ -446,6 +452,7 @@ impl Diagnostic { msg: msg.to_owned(), style: SuggestionStyle::ShowCode, applicability, + tool_metadata: Default::default(), }); self } @@ -515,6 +522,23 @@ impl Diagnostic { self } + /// Adds a suggestion intended only for a tool. The intent is that the metadata encodes + /// the suggestion in a tool-specific way, as it may not even directly involve Rust code. + pub fn tool_only_suggestion_with_metadata( + &mut self, + msg: &str, + applicability: Applicability, + tool_metadata: Json, + ) { + self.suggestions.push(CodeSuggestion { + substitutions: vec![], + msg: msg.to_owned(), + style: SuggestionStyle::CompletelyHidden, + applicability, + tool_metadata: ToolMetadata::new(tool_metadata), + }) + } + pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self { self.span = sp.into(); if let Some(span) = self.span.primary_span() { diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index d57beb1148a..89e844879f7 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -14,6 +14,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap}; use crate::emitter::{Emitter, HumanReadableErrorType}; use crate::registry::Registry; use crate::DiagnosticId; +use crate::ToolMetadata; use crate::{CodeSuggestion, SubDiagnostic}; use rustc_lint_defs::{Applicability, FutureBreakage}; @@ -180,6 +181,8 @@ struct Diagnostic { children: Vec<Diagnostic>, /// The message as rustc would render it. rendered: Option<String>, + /// Extra tool metadata + tool_metadata: ToolMetadata, } #[derive(Encodable)] @@ -269,6 +272,7 @@ impl Diagnostic { spans: DiagnosticSpan::from_suggestion(sugg, je), children: vec![], rendered: None, + tool_metadata: sugg.tool_metadata.clone(), }); // generate regular command line output and store it in the json @@ -312,6 +316,7 @@ impl Diagnostic { .chain(sugg) .collect(), rendered: Some(output), + tool_metadata: ToolMetadata::default(), } } @@ -327,6 +332,7 @@ impl Diagnostic { .unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, je)), children: vec![], rendered: None, + tool_metadata: ToolMetadata::default(), } } } diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index aa882332099..73ddf507723 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -23,10 +23,13 @@ use rustc_data_structures::sync::{self, Lock, Lrc}; use rustc_data_structures::AtomicRef; use rustc_lint_defs::FutureBreakage; pub use rustc_lint_defs::{pluralize, Applicability}; +use rustc_serialize::json::Json; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::SourceMap; use rustc_span::{Loc, MultiSpan, Span}; use std::borrow::Cow; +use std::hash::{Hash, Hasher}; use std::panic; use std::path::Path; use std::{error, fmt}; @@ -73,6 +76,35 @@ impl SuggestionStyle { } } +#[derive(Clone, Debug, PartialEq, Default)] +pub struct ToolMetadata(pub Option<Json>); + +impl ToolMetadata { + fn new(json: Json) -> Self { + ToolMetadata(Some(json)) + } +} + +impl Hash for ToolMetadata { + fn hash<H: Hasher>(&self, _state: &mut H) {} +} + +// Doesn't really need to round-trip +impl<D: Decoder> Decodable<D> for ToolMetadata { + fn decode(_d: &mut D) -> Result<Self, D::Error> { + Ok(ToolMetadata(None)) + } +} + +impl<S: Encoder> Encodable<S> for ToolMetadata { + fn encode(&self, e: &mut S) -> Result<(), S::Error> { + match &self.0 { + None => e.emit_unit(), + Some(json) => json.encode(e), + } + } +} + #[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)] pub struct CodeSuggestion { /// Each substitute can have multiple variants due to multiple @@ -106,6 +138,8 @@ pub struct CodeSuggestion { /// which are useful for users but not useful for /// tools like rustfix pub applicability: Applicability, + /// Tool-specific metadata + pub tool_metadata: ToolMetadata, } #[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)] @@ -775,7 +809,6 @@ impl HandlerInner { } let already_emitted = |this: &mut Self| { - use std::hash::Hash; let mut hasher = StableHasher::new(); diagnostic.hash(&mut hasher); let diagnostic_hash = hasher.finish(); |
