Skip to content

Commit 8826b68

Browse files
committed
Auto merge of rust-lang#109346 - Dylan-DPC:rollup-vszi5bn, r=Dylan-DPC
Rollup of 10 pull requests Successful merges: - rust-lang#104100 (Allow using `Range` as an `Iterator` in const contexts. ) - rust-lang#105793 (Add note for mismatched types because of circular dependencies) - rust-lang#108798 (move default backtrace setting to sys) - rust-lang#108829 (Use Edition 2021 :pat in matches macro) - rust-lang#108973 (Beautify pin! docs) - rust-lang#109003 (Add `useless_anonymous_reexport` lint) - rust-lang#109022 (read_buf_exact: on error, all read bytes are appended to the buffer) - rust-lang#109212 (fix: don't suggest similar method when unstable) - rust-lang#109243 (The name of NativeLib will be presented) - rust-lang#109324 (Implement FixedSizeEncoding for UnusedGenericParams.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ab9bb3e + 881c989 commit 8826b68

File tree

47 files changed

+577
-150
lines changed

Some content is hidden

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

47 files changed

+577
-150
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ fn link_rlib<'a>(
358358
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
359359
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
360360
packed_bundled_libs.push(wrapper_file);
361-
} else if let Some(name) = lib.name {
361+
} else {
362362
let path =
363-
find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
363+
find_native_static_library(lib.name.as_str(), lib.verbatim, &lib_search_paths, sess);
364364
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
365365
sess.emit_fatal(errors::AddNativeLibrary { library_path: path, error })});
366366
}
@@ -436,7 +436,7 @@ fn collate_raw_dylibs<'a, 'b>(
436436
for lib in used_libraries {
437437
if lib.kind == NativeLibKind::RawDylib {
438438
let ext = if lib.verbatim { "" } else { ".dll" };
439-
let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext);
439+
let name = format!("{}{}", lib.name, ext);
440440
let imports = dylib_table.entry(name.clone()).or_default();
441441
for import in &lib.dll_imports {
442442
if let Some(old_import) = imports.insert(import.name, import) {
@@ -1296,7 +1296,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
12961296
.iter()
12971297
.filter(|l| relevant_lib(sess, l))
12981298
.filter_map(|lib| {
1299-
let name = lib.name?;
1299+
let name = lib.name;
13001300
match lib.kind {
13011301
NativeLibKind::Static { bundle: Some(false), .. }
13021302
| NativeLibKind::Dylib { .. }
@@ -1317,6 +1317,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
13171317
// These are included, no need to print them
13181318
NativeLibKind::Static { bundle: None | Some(true), .. }
13191319
| NativeLibKind::LinkArg
1320+
| NativeLibKind::WasmImportModule
13201321
| NativeLibKind::RawDylib => None,
13211322
}
13221323
})
@@ -2275,21 +2276,18 @@ fn add_native_libs_from_crate(
22752276

22762277
let mut last = (None, NativeLibKind::Unspecified, false);
22772278
for lib in native_libs {
2278-
let Some(name) = lib.name else {
2279-
continue;
2280-
};
22812279
if !relevant_lib(sess, lib) {
22822280
continue;
22832281
}
22842282

22852283
// Skip if this library is the same as the last.
2286-
last = if (lib.name, lib.kind, lib.verbatim) == last {
2284+
last = if (Some(lib.name), lib.kind, lib.verbatim) == last {
22872285
continue;
22882286
} else {
2289-
(lib.name, lib.kind, lib.verbatim)
2287+
(Some(lib.name), lib.kind, lib.verbatim)
22902288
};
22912289

2292-
let name = name.as_str();
2290+
let name = lib.name.as_str();
22932291
let verbatim = lib.verbatim;
22942292
match lib.kind {
22952293
NativeLibKind::Static { bundle, whole_archive } => {
@@ -2346,6 +2344,7 @@ fn add_native_libs_from_crate(
23462344
NativeLibKind::RawDylib => {
23472345
// Handled separately in `linker_with_args`.
23482346
}
2347+
NativeLibKind::WasmImportModule => {}
23492348
NativeLibKind::LinkArg => {
23502349
if link_static {
23512350
cmd.arg(name);

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, S
595595

596596
let mut ret = FxHashMap::default();
597597
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
598-
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
598+
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module());
599599
let Some(module) = module else { continue };
600600
ret.extend(lib.foreign_items.iter().map(|id| {
601601
assert_eq!(id.krate, cnum);

compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bitflags::bitflags! {
118118
#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
119119
pub struct NativeLib {
120120
pub kind: NativeLibKind,
121-
pub name: Option<Symbol>,
121+
pub name: Symbol,
122122
pub filename: Option<Symbol>,
123123
pub cfg: Option<ast::MetaItem>,
124124
pub verbatim: bool,

compiler/rustc_hir_typeck/src/method/probe.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10281028
true
10291029
}
10301030
})
1031+
// ensure that we don't suggest unstable methods
1032+
.filter(|candidate| {
1033+
// note that `DUMMY_SP` is ok here because it is only used for
1034+
// suggestions and macro stuff which isn't applicable here.
1035+
!matches!(
1036+
self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1037+
stability::EvalResult::Deny { .. }
1038+
)
1039+
})
10311040
.map(|candidate| candidate.item.ident(self.tcx))
10321041
.filter(|&name| set.insert(name))
10331042
.collect();

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
615615
}
616616

617617
let report_path_match = |err: &mut Diagnostic, did1: DefId, did2: DefId| {
618-
// Only external crates, if either is from a local
619-
// module we could have false positives
620-
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
618+
// Only report definitions from different crates. If both definitions
619+
// are from a local module we could have false positives, e.g.
620+
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
621+
if did1.krate != did2.krate {
621622
let abs_path =
622623
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
623624

@@ -629,10 +630,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
629630
};
630631
if same_path().unwrap_or(false) {
631632
let crate_name = self.tcx.crate_name(did1.krate);
632-
err.note(&format!(
633-
"perhaps two different versions of crate `{}` are being used?",
634-
crate_name
635-
));
633+
let msg = if did1.is_local() || did2.is_local() {
634+
format!(
635+
"the crate `{crate_name}` is compiled multiple times, possibly with different configurations"
636+
)
637+
} else {
638+
format!(
639+
"perhaps two different versions of crate `{crate_name}` are being used?"
640+
)
641+
};
642+
err.note(msg);
636643
}
637644
}
638645
};

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,6 @@ lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its ass
508508
.specifically = this associated type bound is unsatisfied for `{$proj_ty}`
509509
510510
lint_opaque_hidden_inferred_bound_sugg = add this bound
511+
512+
lint_useless_anonymous_reexport = useless anonymous re-export
513+
.note = only anonymous re-exports of traits are useful, this is {$article} `{$desc}`

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mod opaque_hidden_inferred_bound;
7474
mod pass_by_value;
7575
mod passes;
7676
mod redundant_semicolon;
77+
mod reexports;
7778
mod traits;
7879
mod types;
7980
mod unused;
@@ -111,6 +112,7 @@ use noop_method_call::*;
111112
use opaque_hidden_inferred_bound::*;
112113
use pass_by_value::*;
113114
use redundant_semicolon::*;
115+
use reexports::*;
114116
use traits::*;
115117
use types::*;
116118
use unused::*;
@@ -242,6 +244,7 @@ late_lint_methods!(
242244
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
243245
MultipleSupertraitUpcastable: MultipleSupertraitUpcastable,
244246
MapUnitFn: MapUnitFn,
247+
UselessAnonymousReexport: UselessAnonymousReexport,
245248
]
246249
]
247250
);

compiler/rustc_lint/src/lints.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,11 @@ pub struct UnusedAllocationDiag;
15281528
#[derive(LintDiagnostic)]
15291529
#[diag(lint_unused_allocation_mut)]
15301530
pub struct UnusedAllocationMutDiag;
1531+
1532+
#[derive(LintDiagnostic)]
1533+
#[diag(lint_useless_anonymous_reexport)]
1534+
#[note]
1535+
pub struct UselessAnonymousReexportDiag {
1536+
pub article: &'static str,
1537+
pub desc: &'static str,
1538+
}

compiler/rustc_lint/src/reexports.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::lints::UselessAnonymousReexportDiag;
2+
use crate::{LateContext, LateLintPass, LintContext};
3+
use rustc_hir::def::DefKind;
4+
use rustc_hir::def_id::DefId;
5+
use rustc_hir::{Item, ItemKind, UseKind};
6+
use rustc_middle::ty::Visibility;
7+
use rustc_span::symbol::kw;
8+
use rustc_span::Span;
9+
10+
declare_lint! {
11+
/// The `useless_anonymous_reexport` lint checks if anonymous re-exports
12+
/// are re-exports of traits.
13+
///
14+
/// ### Example
15+
///
16+
/// ```rust,compile_fail
17+
/// #![deny(useless_anonymous_reexport)]
18+
///
19+
/// mod sub {
20+
/// pub struct Bar;
21+
/// }
22+
///
23+
/// pub use self::sub::Bar as _;
24+
/// # fn main() {}
25+
/// ```
26+
///
27+
/// {{produces}}
28+
///
29+
/// ### Explanation
30+
///
31+
/// Anonymous re-exports are only useful if it's a re-export of a trait
32+
/// in case you want to give access to it. If you re-export any other kind,
33+
/// you won't be able to use it since its name won't be accessible.
34+
pub USELESS_ANONYMOUS_REEXPORT,
35+
Warn,
36+
"useless anonymous re-export"
37+
}
38+
39+
declare_lint_pass!(UselessAnonymousReexport => [USELESS_ANONYMOUS_REEXPORT]);
40+
41+
fn emit_err(cx: &LateContext<'_>, span: Span, def_id: DefId) {
42+
let article = cx.tcx.def_descr_article(def_id);
43+
let desc = cx.tcx.def_descr(def_id);
44+
cx.emit_spanned_lint(
45+
USELESS_ANONYMOUS_REEXPORT,
46+
span,
47+
UselessAnonymousReexportDiag { article, desc },
48+
);
49+
}
50+
51+
impl<'tcx> LateLintPass<'tcx> for UselessAnonymousReexport {
52+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
53+
if let ItemKind::Use(path, kind) = item.kind &&
54+
!matches!(kind, UseKind::Glob) &&
55+
item.ident.name == kw::Underscore &&
56+
// We only want re-exports. If it's just a `use X;`, then we ignore it.
57+
match cx.tcx.local_visibility(item.owner_id.def_id) {
58+
Visibility::Public => true,
59+
Visibility::Restricted(level) => {
60+
level != cx.tcx.parent_module_from_def_id(item.owner_id.def_id)
61+
}
62+
}
63+
{
64+
for def_id in path.res.iter().filter_map(|r| r.opt_def_id()) {
65+
match cx.tcx.def_kind(def_id) {
66+
DefKind::Trait | DefKind::TraitAlias => {}
67+
DefKind::TyAlias => {
68+
let ty = cx.tcx.type_of(def_id);
69+
if !ty.0.is_trait() {
70+
emit_err(cx, item.span, def_id);
71+
break;
72+
}
73+
}
74+
_ => {
75+
emit_err(cx, item.span, def_id);
76+
break;
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)