Skip to content

Commit 7281249

Browse files
committed
Auto merge of rust-lang#108538 - matthiaskrgr:rollup-vw6h5ea, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#104265 (Move IpAddr, SocketAddr and V4+V6 related types to `core`) - rust-lang#107110 ([stdio][windows] Use MBTWC and WCTMB) - rust-lang#108308 (Allow building serde and serde_derive in parallel) - rust-lang#108363 (Move the unused extern crate check back to the resolver.) - rust-lang#108519 (Bages for easy access links to Rust community) - rust-lang#108522 (Commit some new solver tests) - rust-lang#108523 (Avoid `&str` to `String` conversions) - rust-lang#108533 (diagnostics: avoid querying `associated_item` in the resolver) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents f540a25 + a184150 commit 7281249

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4449
-4024
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# The Rust Programming Language
22

3+
[![Rust Community](https://img.shields.io/badge/Rust_Community%20-Join_us-brightgreen?style=plastic&logo=rust)](https://www.rust-lang.org/community)
4+
35
This is the main source code repository for [Rust]. It contains the compiler,
46
standard library, and documentation.
57

compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
386386
).span_suggestion_verbose(
387387
lhs.span.shrink_to_lo(),
388388
"you might have meant to introduce a new binding",
389-
"let ".to_string(),
389+
"let ",
390390
Applicability::MachineApplicable,
391391
).emit();
392392
}

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
467467
err.span_suggestion_verbose(
468468
span.shrink_to_lo(),
469469
"consider borrowing here",
470-
"&".to_string(),
470+
'&',
471471
Applicability::MaybeIncorrect,
472472
);
473473
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
385385
err.span_suggestion_verbose(
386386
local_decl.source_info.span.shrink_to_lo(),
387387
"consider changing this to be mutable",
388-
"mut ".to_string(),
388+
"mut ",
389389
Applicability::MachineApplicable,
390390
);
391391
let tcx = self.infcx.tcx;

compiler/rustc_hir_analysis/locales/en-US.ftl

-8
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ hir_analysis_manual_implementation =
6262
6363
hir_analysis_substs_on_overridden_impl = could not resolve substs on overridden impl
6464
65-
hir_analysis_unused_extern_crate =
66-
unused extern crate
67-
.suggestion = remove it
68-
69-
hir_analysis_extern_crate_not_idiomatic =
70-
`extern crate` is not idiomatic in the new edition
71-
.suggestion = convert it to a `{$msg_code}`
72-
7365
hir_analysis_trait_object_declared_with_no_traits =
7466
at least one trait is required for an object type
7567
.alias_span = this alias does not contain a trait
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate};
2-
use rustc_data_structures::fx::FxHashMap;
31
use rustc_data_structures::unord::UnordSet;
4-
use rustc_hir as hir;
52
use rustc_hir::def::DefKind;
6-
use rustc_hir::def_id::{DefId, LocalDefId};
3+
use rustc_hir::def_id::LocalDefId;
74
use rustc_middle::ty::TyCtxt;
85
use rustc_session::lint;
9-
use rustc_span::{Span, Symbol};
106

117
pub fn check_crate(tcx: TyCtxt<'_>) {
128
let mut used_trait_imports: UnordSet<LocalDefId> = Default::default();
@@ -43,131 +39,4 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
4339
|lint| lint,
4440
);
4541
}
46-
47-
unused_crates_lint(tcx);
48-
}
49-
50-
fn unused_crates_lint(tcx: TyCtxt<'_>) {
51-
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
52-
53-
// Collect first the crates that are completely unused. These we
54-
// can always suggest removing (no matter which edition we are
55-
// in).
56-
let unused_extern_crates: FxHashMap<LocalDefId, Span> = tcx
57-
.maybe_unused_extern_crates(())
58-
.iter()
59-
.filter(|&&(def_id, _)| {
60-
tcx.extern_mod_stmt_cnum(def_id).map_or(true, |cnum| {
61-
!tcx.is_compiler_builtins(cnum)
62-
&& !tcx.is_panic_runtime(cnum)
63-
&& !tcx.has_global_allocator(cnum)
64-
&& !tcx.has_panic_handler(cnum)
65-
})
66-
})
67-
.cloned()
68-
.collect();
69-
70-
// Collect all the extern crates (in a reliable order).
71-
let mut crates_to_lint = vec![];
72-
73-
for id in tcx.hir().items() {
74-
if matches!(tcx.def_kind(id.owner_id), DefKind::ExternCrate) {
75-
let item = tcx.hir().item(id);
76-
if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
77-
crates_to_lint.push(ExternCrateToLint {
78-
def_id: item.owner_id.to_def_id(),
79-
span: item.span,
80-
orig_name,
81-
warn_if_unused: !item.ident.as_str().starts_with('_'),
82-
});
83-
}
84-
}
85-
}
86-
87-
let extern_prelude = &tcx.resolutions(()).extern_prelude;
88-
89-
for extern_crate in &crates_to_lint {
90-
let def_id = extern_crate.def_id.expect_local();
91-
let item = tcx.hir().expect_item(def_id);
92-
93-
// If the crate is fully unused, we suggest removing it altogether.
94-
// We do this in any edition.
95-
if extern_crate.warn_if_unused {
96-
if let Some(&span) = unused_extern_crates.get(&def_id) {
97-
// Removal suggestion span needs to include attributes (Issue #54400)
98-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
99-
let span_with_attrs = tcx
100-
.hir()
101-
.attrs(id)
102-
.iter()
103-
.map(|attr| attr.span)
104-
.fold(span, |acc, attr_span| acc.to(attr_span));
105-
106-
tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs });
107-
continue;
108-
}
109-
}
110-
111-
// If we are not in Rust 2018 edition, then we don't make any further
112-
// suggestions.
113-
if !tcx.sess.rust_2018() {
114-
continue;
115-
}
116-
117-
// If the extern crate isn't in the extern prelude,
118-
// there is no way it can be written as a `use`.
119-
let orig_name = extern_crate.orig_name.unwrap_or(item.ident.name);
120-
if !extern_prelude.get(&orig_name).map_or(false, |from_item| !from_item) {
121-
continue;
122-
}
123-
124-
// If the extern crate is renamed, then we cannot suggest replacing it with a use as this
125-
// would not insert the new name into the prelude, where other imports in the crate may be
126-
// expecting it.
127-
if extern_crate.orig_name.is_some() {
128-
continue;
129-
}
130-
131-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
132-
// If the extern crate has any attributes, they may have funky
133-
// semantics we can't faithfully represent using `use` (most
134-
// notably `#[macro_use]`). Ignore it.
135-
if !tcx.hir().attrs(id).is_empty() {
136-
continue;
137-
}
138-
139-
let base_replacement = match extern_crate.orig_name {
140-
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
141-
None => format!("use {};", item.ident.name),
142-
};
143-
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
144-
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
145-
tcx.emit_spanned_lint(
146-
lint,
147-
id,
148-
extern_crate.span,
149-
ExternCrateNotIdiomatic {
150-
span: extern_crate.span,
151-
msg_code: add_vis("use".to_string()),
152-
suggestion_code: add_vis(base_replacement),
153-
},
154-
);
155-
}
156-
}
157-
158-
struct ExternCrateToLint {
159-
/// `DefId` of the extern crate
160-
def_id: DefId,
161-
162-
/// span from the item
163-
span: Span,
164-
165-
/// if `Some`, then this is renamed (`extern crate orig_name as
166-
/// crate_name`), and -- perhaps surprisingly -- this stores the
167-
/// *original* name (`item.name` will contain the new name)
168-
orig_name: Option<Symbol>,
169-
170-
/// if `false`, the original name started with `_`, so we shouldn't lint
171-
/// about it going unused (but we should still emit idiom lints).
172-
warn_if_unused: bool,
17342
}

compiler/rustc_hir_analysis/src/errors.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{
55
error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
66
MultiSpan,
77
};
8-
use rustc_macros::{Diagnostic, LintDiagnostic};
8+
use rustc_macros::Diagnostic;
99
use rustc_middle::ty::Ty;
1010
use rustc_span::{symbol::Ident, Span, Symbol};
1111

@@ -247,26 +247,6 @@ pub struct SubstsOnOverriddenImpl {
247247
pub span: Span,
248248
}
249249

250-
#[derive(LintDiagnostic)]
251-
#[diag(hir_analysis_unused_extern_crate)]
252-
pub struct UnusedExternCrate {
253-
#[suggestion(applicability = "machine-applicable", code = "")]
254-
pub span: Span,
255-
}
256-
257-
#[derive(LintDiagnostic)]
258-
#[diag(hir_analysis_extern_crate_not_idiomatic)]
259-
pub struct ExternCrateNotIdiomatic {
260-
#[suggestion(
261-
style = "short",
262-
applicability = "machine-applicable",
263-
code = "{suggestion_code}"
264-
)]
265-
pub span: Span,
266-
pub msg_code: String,
267-
pub suggestion_code: String,
268-
}
269-
270250
#[derive(Diagnostic)]
271251
#[diag(hir_analysis_const_impl_for_non_const_trait)]
272252
pub struct ConstImplForNonConstTrait {

compiler/rustc_lint/src/context.rs

+17
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,23 @@ pub trait LintContext: Sized {
893893
BuiltinLintDiagnostics::ByteSliceInPackedStructWithDerive => {
894894
db.help("consider implementing the trait by hand, or remove the `packed` attribute");
895895
}
896+
BuiltinLintDiagnostics::UnusedExternCrate { removal_span }=> {
897+
db.span_suggestion(
898+
removal_span,
899+
"remove it",
900+
"",
901+
Applicability::MachineApplicable,
902+
);
903+
}
904+
BuiltinLintDiagnostics::ExternCrateNotIdiomatic { vis_span, ident_span }=> {
905+
let suggestion_span = vis_span.between(ident_span);
906+
db.span_suggestion_verbose(
907+
suggestion_span,
908+
"convert it to a `use`",
909+
if vis_span.is_empty() { "use " } else { " use " },
910+
Applicability::MachineApplicable,
911+
);
912+
}
896913
}
897914
// Rewrap `db`, and pass control to the user.
898915
decorate(db)

compiler/rustc_lint_defs/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ pub enum BuiltinLintDiagnostics {
522522
is_formatting_arg: bool,
523523
},
524524
ByteSliceInPackedStructWithDerive,
525+
UnusedExternCrate {
526+
removal_span: Span,
527+
},
528+
ExternCrateNotIdiomatic {
529+
vis_span: Span,
530+
ident_span: Span,
531+
},
525532
}
526533

527534
/// Lints that are buffered up early on in the `Session` before the

compiler/rustc_middle/src/query/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,6 @@ rustc_queries! {
18301830
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
18311831
desc { "fetching potentially unused trait imports" }
18321832
}
1833-
query maybe_unused_extern_crates(_: ()) -> &'tcx [(LocalDefId, Span)] {
1834-
desc { "looking up all possibly unused extern crates" }
1835-
}
18361833
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> {
18371834
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
18381835
}

compiler/rustc_middle/src/ty/context.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2487,8 +2487,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
24872487
|tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]);
24882488
providers.maybe_unused_trait_imports =
24892489
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
2490-
providers.maybe_unused_extern_crates =
2491-
|tcx, ()| &tcx.resolutions(()).maybe_unused_extern_crates[..];
24922490
providers.names_imported_by_glob_use = |tcx, id| {
24932491
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
24942492
};

compiler/rustc_middle/src/ty/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ pub struct ResolverGlobalCtxt {
165165
pub effective_visibilities: EffectiveVisibilities,
166166
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
167167
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
168-
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
169168
pub reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
170169
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
171-
/// Extern prelude entries. The value is `true` if the entry was introduced
172-
/// via `extern crate` item and not `--extern` option or compiler built-in.
173-
pub extern_prelude: FxHashMap<Symbol, bool>,
174170
pub main_def: Option<MainDefinition>,
175171
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
176172
/// A list of proc macro LocalDefIds, written out in the order in which

0 commit comments

Comments
 (0)