Skip to content

Commit

Permalink
Rollup merge of #96086 - jsgf:remove-extern-location, r=davidtwco
Browse files Browse the repository at this point in the history
Remove `--extern-location` and all associated code

`--extern-location` was an experiment to investigate the best way to
generate useful diagnostics for unused dependency warnings by enabling a
build system to identify the corresponding build config.

While I did successfully use this, I've since been convinced the
alternative `--json unused-externs` mechanism is the way to go, and
there's no point in having two mechanisms with basically the same
functionality.

This effectively reverts #72603
  • Loading branch information
Dylan-DPC authored Apr 19, 2022
2 parents 9a2d14c + 1be1157 commit 5f10d13
Show file tree
Hide file tree
Showing 36 changed files with 6 additions and 517 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3934,7 +3934,6 @@ dependencies = [
"rustc_infer",
"rustc_middle",
"rustc_parse_format",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down
25 changes: 1 addition & 24 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::snippet::Style;
use crate::{
CodeSuggestion, DiagnosticMessage, Level, MultiSpan, Substitution, SubstitutionPart,
SuggestionStyle, ToolMetadata,
SuggestionStyle,
};
use rustc_data_structures::stable_map::FxHashMap;
use rustc_error_messages::FluentValue;
use rustc_lint_defs::{Applicability, LintExpectationId};
use rustc_serialize::json::Json;
use rustc_span::edition::LATEST_STABLE_EDITION;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -554,7 +553,6 @@ impl Diagnostic {
msg: msg.into(),
style,
applicability,
tool_metadata: Default::default(),
});
self
}
Expand Down Expand Up @@ -582,7 +580,6 @@ impl Diagnostic {
msg: msg.into(),
style: SuggestionStyle::CompletelyHidden,
applicability,
tool_metadata: Default::default(),
});
self
}
Expand Down Expand Up @@ -637,7 +634,6 @@ impl Diagnostic {
msg: msg.into(),
style,
applicability,
tool_metadata: Default::default(),
});
self
}
Expand Down Expand Up @@ -680,7 +676,6 @@ impl Diagnostic {
msg: msg.into(),
style: SuggestionStyle::ShowCode,
applicability,
tool_metadata: Default::default(),
});
self
}
Expand All @@ -705,7 +700,6 @@ impl Diagnostic {
msg: msg.into(),
style: SuggestionStyle::ShowCode,
applicability,
tool_metadata: Default::default(),
});
self
}
Expand Down Expand Up @@ -774,23 +768,6 @@ 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: impl Into<DiagnosticMessage>,
applicability: Applicability,
tool_metadata: Json,
) {
self.push_suggestion(CodeSuggestion {
substitutions: vec![],
msg: msg.into(),
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() {
Expand Down
67 changes: 1 addition & 66 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
};
Expand All @@ -30,7 +29,6 @@ use std::sync::{Arc, Mutex};
use std::vec;

use rustc_serialize::json::{as_json, as_pretty_json};
use rustc_serialize::{Encodable, Encoder};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -205,8 +203,7 @@ impl Emitter for JsonEmitter {

// The following data types are provided just for serialisation.

// NOTE: this has a manual implementation of Encodable which needs to be updated in
// parallel.
#[derive(Encodable)]
struct Diagnostic {
/// The primary error message.
message: String,
Expand All @@ -218,65 +215,6 @@ struct Diagnostic {
children: Vec<Diagnostic>,
/// The message as rustc would render it.
rendered: Option<String>,
/// Extra tool metadata
tool_metadata: ToolMetadata,
}

macro_rules! encode_fields {
(
$enc:expr, // encoder
$idx:expr, // starting field index
$struct:expr, // struct we're serializing
$struct_name:ident, // struct name
[ $($name:ident),+$(,)? ], // fields to encode
[ $($ignore:ident),+$(,)? ] // fields we're skipping
) => {
{
// Pattern match to make sure all fields are accounted for
let $struct_name { $($name,)+ $($ignore: _,)+ } = $struct;
let mut idx = $idx;
$(
$enc.emit_struct_field(
stringify!($name),
idx == 0,
|enc| $name.encode(enc),
)?;
idx += 1;
)+
idx
}
};
}

// Special-case encoder to skip tool_metadata if not set
impl<E: Encoder> Encodable<E> for Diagnostic {
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_struct(false, |s| {
let mut idx = 0;

idx = encode_fields!(
s,
idx,
self,
Self,
[message, code, level, spans, children, rendered],
[tool_metadata]
);
if self.tool_metadata.is_set() {
idx = encode_fields!(
s,
idx,
self,
Self,
[tool_metadata],
[message, code, level, spans, children, rendered]
);
}

let _ = idx;
Ok(())
})
}
}

#[derive(Encodable)]
Expand Down Expand Up @@ -380,7 +318,6 @@ impl Diagnostic {
spans: DiagnosticSpan::from_suggestion(sugg, &args, je),
children: vec![],
rendered: None,
tool_metadata: sugg.tool_metadata.clone(),
}
});

Expand Down Expand Up @@ -428,7 +365,6 @@ impl Diagnostic {
.chain(sugg)
.collect(),
rendered: Some(output),
tool_metadata: ToolMetadata::default(),
}
}

Expand All @@ -449,7 +385,6 @@ impl Diagnostic {
.unwrap_or_else(|| DiagnosticSpan::from_multispan(&diag.span, args, je)),
children: vec![],
rendered: None,
tool_metadata: ToolMetadata::default(),
}
}
}
Expand Down
39 changes: 1 addition & 38 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ pub use rustc_error_messages::{
LazyFallbackBundle, MultiSpan, SpanLabel, DEFAULT_LOCALE_RESOURCES,
};
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, Span};

use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::panic;
use std::path::Path;
Expand Down Expand Up @@ -93,39 +91,6 @@ impl SuggestionStyle {
}
}

#[derive(Clone, Debug, PartialEq, Default)]
pub struct ToolMetadata(pub Option<Json>);

impl ToolMetadata {
fn new(json: Json) -> Self {
ToolMetadata(Some(json))
}

fn is_set(&self) -> bool {
self.0.is_some()
}
}

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) -> Self {
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
Expand Down Expand Up @@ -159,8 +124,6 @@ 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)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_feature = { path = "../rustc_feature" }
rustc_index = { path = "../rustc_index" }
rustc_session = { path = "../rustc_session" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
rustc_parse_format = { path = "../rustc_parse_format" }
rustc_infer = { path = "../rustc_infer" }
27 changes: 1 addition & 26 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use rustc_middle::middle::stability;
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, RegisteredTools, Ty, TyCtxt};
use rustc_serialize::json::Json;
use rustc_session::lint::{BuiltinLintDiagnostics, ExternDepSpec};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
use rustc_session::Session;
use rustc_span::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -728,30 +727,6 @@ pub trait LintContext: Sized {
BuiltinLintDiagnostics::LegacyDeriveHelpers(span) => {
db.span_label(span, "the attribute is introduced here");
}
BuiltinLintDiagnostics::ExternDepSpec(krate, loc) => {
let json = match loc {
ExternDepSpec::Json(json) => {
db.help(&format!("remove unnecessary dependency `{}`", krate));
json
}
ExternDepSpec::Raw(raw) => {
db.help(&format!("remove unnecessary dependency `{}` at `{}`", krate, raw));
db.span_suggestion_with_style(
DUMMY_SP,
"raw extern location",
raw.clone(),
Applicability::Unspecified,
SuggestionStyle::CompletelyHidden,
);
Json::String(raw)
}
};
db.tool_only_suggestion_with_metadata(
"json extern location",
Applicability::Unspecified,
json
);
}
BuiltinLintDiagnostics::ProcMacroBackCompat(note) => {
db.note(&note);
}
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_ast::{AttrId, Attribute};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_error_messages::MultiSpan;
use rustc_hir::HirId;
use rustc_serialize::json::Json;
use rustc_span::edition::Edition;
use rustc_span::{sym, symbol::Ident, Span, Symbol};
use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -403,13 +402,6 @@ impl<HCX> ToStableHashKey<HCX> for LintId {
}
}

// Duplicated from rustc_session::config::ExternDepSpec to avoid cyclic dependency
#[derive(PartialEq, Debug)]
pub enum ExternDepSpec {
Json(Json),
Raw(String),
}

// This could be a closure, but then implementing derive trait
// becomes hacky (and it gets allocated).
#[derive(Debug)]
Expand All @@ -428,7 +420,6 @@ pub enum BuiltinLintDiagnostics {
UnusedBuiltinAttribute { attr_name: Symbol, macro_name: String, invoc_span: Span },
PatternsInFnsWithoutBody(Span, Ident),
LegacyDeriveHelpers(Span),
ExternDepSpec(String, ExternDepSpec),
ProcMacroBackCompat(String),
OrPatternsBackCompat(Span, String),
ReservedPrefix(Span),
Expand Down
20 changes: 2 additions & 18 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_index::vec::IndexVec;
use rustc_middle::ty::TyCtxt;
use rustc_serialize::json::ToJson;
use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate};
use rustc_session::cstore::{ExternCrateSource, MetadataLoaderDyn};
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
use rustc_session::lint;
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_session::Session;
Expand All @@ -27,7 +26,6 @@ use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::{PanicStrategy, TargetTriple};

use proc_macro::bridge::client::ProcMacro;
use std::collections::BTreeMap;
use std::ops::Fn;
use std::path::Path;
use std::{cmp, env};
Expand Down Expand Up @@ -920,20 +918,7 @@ impl<'a> CrateLoader<'a> {
continue;
}

let diag = match self.sess.opts.extern_dep_specs.get(name) {
Some(loc) => BuiltinLintDiagnostics::ExternDepSpec(name.clone(), loc.into()),
None => {
// If we don't have a specific location, provide a json encoding of the `--extern`
// option.
let meta: BTreeMap<String, String> =
std::iter::once(("name".to_string(), name.to_string())).collect();
BuiltinLintDiagnostics::ExternDepSpec(
name.clone(),
ExternDepSpec::Json(meta.to_json()),
)
}
};
self.sess.parse_sess.buffer_lint_with_diagnostic(
self.sess.parse_sess.buffer_lint(
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
span,
ast::CRATE_NODE_ID,
Expand All @@ -942,7 +927,6 @@ impl<'a> CrateLoader<'a> {
name,
self.local_crate_name,
name),
diag,
);
}
}
Expand Down
Loading

0 comments on commit 5f10d13

Please sign in to comment.