Skip to content

Commit 6de065d

Browse files
committed
Auto merge of rust-lang#120279 - fmease:rollup-4nhar13, r=fmease
Rollup of 12 pull requests Successful merges: - rust-lang#112806 (Small code improvements in `collect_intra_doc_links.rs`) - rust-lang#119460 (coverage: Never emit improperly-ordered coverage regions) - rust-lang#119766 (Split tait and impl trait in assoc items logic) - rust-lang#120062 (llvm: change data layout bug to an error and make it trigger more) - rust-lang#120099 (linker: Refactor library linking methods in `trait Linker`) - rust-lang#120139 (Do not normalize closure signature when building `FnOnce` shim) - rust-lang#120160 (Manually implement derived `NonZero` traits.) - rust-lang#120171 (Fix assume and assert in jump threading) - rust-lang#120183 (Add `#[coverage(off)]` to closures introduced by `#[test]` and `#[bench]`) - rust-lang#120195 (add several resolution test cases) - rust-lang#120259 (Split Diagnostics for Uncommon Codepoints: Add List to Display Characters Involved) - rust-lang#120261 (Provide structured suggestion to use trait objects in some cases of `if` arm type divergence) r? `@ghost` `@rustbot` modify labels: rollup
2 parents dfe53af + 4756975 commit 6de065d

File tree

99 files changed

+1950
-559
lines changed

Some content is hidden

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

99 files changed

+1950
-559
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![doc(rust_logo)]
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(array_windows)]
9+
#![feature(assert_matches)]
910
#![feature(box_patterns)]
1011
#![feature(decl_macro)]
1112
#![feature(if_let_guard)]

compiler/rustc_builtin_macros/src/test.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Level};
99
use rustc_expand::base::*;
1010
use rustc_span::symbol::{sym, Ident, Symbol};
1111
use rustc_span::{ErrorGuaranteed, FileNameDisplayPreference, Span};
12+
use std::assert_matches::assert_matches;
1213
use std::iter;
1314
use thin_vec::{thin_vec, ThinVec};
1415

@@ -182,6 +183,16 @@ pub fn expand_test_or_bench(
182183
// creates $name: $expr
183184
let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
184185

186+
// Adds `#[coverage(off)]` to a closure, so it won't be instrumented in
187+
// `-Cinstrument-coverage` builds.
188+
// This requires `#[allow_internal_unstable(coverage_attribute)]` on the
189+
// corresponding macro declaration in `core::macros`.
190+
let coverage_off = |mut expr: P<ast::Expr>| {
191+
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
192+
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
193+
expr
194+
};
195+
185196
let test_fn = if is_bench {
186197
// A simple ident for a lambda
187198
let b = Ident::from_str_and_span("b", attr_sp);
@@ -190,8 +201,9 @@ pub fn expand_test_or_bench(
190201
sp,
191202
cx.expr_path(test_path("StaticBenchFn")),
192203
thin_vec![
204+
// #[coverage(off)]
193205
// |b| self::test::assert_test_result(
194-
cx.lambda1(
206+
coverage_off(cx.lambda1(
195207
sp,
196208
cx.expr_call(
197209
sp,
@@ -206,16 +218,17 @@ pub fn expand_test_or_bench(
206218
],
207219
),
208220
b,
209-
), // )
221+
)), // )
210222
],
211223
)
212224
} else {
213225
cx.expr_call(
214226
sp,
215227
cx.expr_path(test_path("StaticTestFn")),
216228
thin_vec![
229+
// #[coverage(off)]
217230
// || {
218-
cx.lambda0(
231+
coverage_off(cx.lambda0(
219232
sp,
220233
// test::assert_test_result(
221234
cx.expr_call(
@@ -230,7 +243,7 @@ pub fn expand_test_or_bench(
230243
), // )
231244
],
232245
), // }
233-
), // )
246+
)), // )
234247
],
235248
)
236249
};

compiler/rustc_codegen_cranelift/src/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ fn codegen_stmt<'tcx>(
682682
args,
683683
ty::ClosureKind::FnOnce,
684684
)
685-
.expect("failed to normalize and resolve closure during codegen")
686685
.polymorphize(fx.tcx);
687686
let func_ref = fx.get_function_ref(instance);
688687
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);

compiler/rustc_codegen_llvm/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ codegen_llvm_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdy
3939
4040
codegen_llvm_lto_proc_macro = lto cannot be used for `proc-macro` crate type without `-Zdylib-lto`
4141
42+
codegen_llvm_mismatch_data_layout =
43+
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
44+
4245
codegen_llvm_missing_features =
4346
add the missing features in a `target_feature` attribute
4447

compiler/rustc_codegen_llvm/src/context.rs

+9-29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3434
use smallvec::SmallVec;
3535

3636
use libc::c_uint;
37+
use std::borrow::Borrow;
3738
use std::cell::{Cell, RefCell};
3839
use std::ffi::CStr;
3940
use std::str;
@@ -155,42 +156,21 @@ pub unsafe fn create_module<'ll>(
155156
}
156157

157158
// Ensure the data-layout values hardcoded remain the defaults.
158-
if sess.target.is_builtin {
159-
// tm is disposed by its drop impl
159+
{
160160
let tm = crate::back::write::create_informational_target_machine(tcx.sess);
161161
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, &tm);
162162

163163
let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
164164
let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
165165
.expect("got a non-UTF8 data-layout from LLVM");
166166

167-
// Unfortunately LLVM target specs change over time, and right now we
168-
// don't have proper support to work with any more than one
169-
// `data_layout` than the one that is in the rust-lang/rust repo. If
170-
// this compiler is configured against a custom LLVM, we may have a
171-
// differing data layout, even though we should update our own to use
172-
// that one.
173-
//
174-
// As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
175-
// disable this check entirely as we may be configured with something
176-
// that has a different target layout.
177-
//
178-
// Unsure if this will actually cause breakage when rustc is configured
179-
// as such.
180-
//
181-
// FIXME(#34960)
182-
let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
183-
let custom_llvm_used = !cfg_llvm_root.trim().is_empty();
184-
185-
if !custom_llvm_used && target_data_layout != llvm_data_layout {
186-
bug!(
187-
"data-layout for target `{rustc_target}`, `{rustc_layout}`, \
188-
differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
189-
rustc_target = sess.opts.target_triple,
190-
rustc_layout = target_data_layout,
191-
llvm_target = sess.target.llvm_target,
192-
llvm_layout = llvm_data_layout
193-
);
167+
if target_data_layout != llvm_data_layout {
168+
tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
169+
rustc_target: sess.opts.target_triple.to_string().as_str(),
170+
rustc_layout: target_data_layout.as_str(),
171+
llvm_target: sess.target.llvm_target.borrow(),
172+
llvm_layout: llvm_data_layout,
173+
});
194174
}
195175
}
196176

compiler/rustc_codegen_llvm/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,12 @@ pub(crate) struct CopyBitcode {
244244
pub struct UnknownCompression {
245245
pub algorithm: &'static str,
246246
}
247+
248+
#[derive(Diagnostic)]
249+
#[diag(codegen_llvm_mismatch_data_layout)]
250+
pub struct MismatchedDataLayout<'a> {
251+
pub rustc_target: &'a str,
252+
pub rustc_layout: &'a str,
253+
pub llvm_target: &'a str,
254+
pub llvm_layout: &'a str,
255+
}

compiler/rustc_codegen_ssa/src/back/link.rs

+31-27
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ use std::path::{Path, PathBuf};
5252
use std::process::{ExitStatus, Output, Stdio};
5353
use std::{env, fmt, fs, io, mem, str};
5454

55+
#[derive(Default)]
56+
pub struct SearchPaths(OnceCell<Vec<PathBuf>>);
57+
58+
impl SearchPaths {
59+
pub(super) fn get(&self, sess: &Session) -> &[PathBuf] {
60+
self.0.get_or_init(|| archive_search_paths(sess))
61+
}
62+
}
63+
5564
pub fn ensure_removed(dcx: &DiagCtxt, path: &Path) {
5665
if let Err(e) = fs::remove_file(path) {
5766
if e.kind() != io::ErrorKind::NotFound {
@@ -1265,15 +1274,15 @@ fn link_sanitizer_runtime(
12651274
let path = find_sanitizer_runtime(sess, &filename);
12661275
let rpath = path.to_str().expect("non-utf8 component in path");
12671276
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
1268-
linker.link_dylib(&filename, false, true);
1277+
linker.link_dylib_by_name(&filename, false, true);
12691278
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
12701279
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
12711280
// compatible ASAN library.
12721281
linker.arg("/INFERASANLIBS");
12731282
} else {
12741283
let filename = format!("librustc{channel}_rt.{name}.a");
12751284
let path = find_sanitizer_runtime(sess, &filename).join(&filename);
1276-
linker.link_whole_rlib(&path);
1285+
linker.link_staticlib_by_path(&path, true);
12771286
}
12781287
}
12791288

@@ -2445,7 +2454,7 @@ fn add_native_libs_from_crate(
24452454
archive_builder_builder: &dyn ArchiveBuilderBuilder,
24462455
codegen_results: &CodegenResults,
24472456
tmpdir: &Path,
2448-
search_paths: &OnceCell<Vec<PathBuf>>,
2457+
search_paths: &SearchPaths,
24492458
bundled_libs: &FxHashSet<Symbol>,
24502459
cnum: CrateNum,
24512460
link_static: bool,
@@ -2505,46 +2514,34 @@ fn add_native_libs_from_crate(
25052514
if let Some(filename) = lib.filename {
25062515
// If rlib contains native libs as archives, they are unpacked to tmpdir.
25072516
let path = tmpdir.join(filename.as_str());
2508-
if whole_archive {
2509-
cmd.link_whole_rlib(&path);
2510-
} else {
2511-
cmd.link_rlib(&path);
2512-
}
2517+
cmd.link_staticlib_by_path(&path, whole_archive);
25132518
}
25142519
} else {
2515-
if whole_archive {
2516-
cmd.link_whole_staticlib(
2517-
name,
2518-
verbatim,
2519-
search_paths.get_or_init(|| archive_search_paths(sess)),
2520-
);
2521-
} else {
2522-
cmd.link_staticlib(name, verbatim)
2523-
}
2520+
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
25242521
}
25252522
}
25262523
}
25272524
NativeLibKind::Dylib { as_needed } => {
25282525
if link_dynamic {
2529-
cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
2526+
cmd.link_dylib_by_name(name, verbatim, as_needed.unwrap_or(true))
25302527
}
25312528
}
25322529
NativeLibKind::Unspecified => {
25332530
// If we are generating a static binary, prefer static library when the
25342531
// link kind is unspecified.
25352532
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
25362533
if link_static {
2537-
cmd.link_staticlib(name, verbatim)
2534+
cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
25382535
}
25392536
} else {
25402537
if link_dynamic {
2541-
cmd.link_dylib(name, verbatim, true);
2538+
cmd.link_dylib_by_name(name, verbatim, true);
25422539
}
25432540
}
25442541
}
25452542
NativeLibKind::Framework { as_needed } => {
25462543
if link_dynamic {
2547-
cmd.link_framework(name, as_needed.unwrap_or(true))
2544+
cmd.link_framework_by_name(name, verbatim, as_needed.unwrap_or(true))
25482545
}
25492546
}
25502547
NativeLibKind::RawDylib => {
@@ -2581,7 +2578,7 @@ fn add_local_native_libraries(
25812578
}
25822579
}
25832580

2584-
let search_paths = OnceCell::new();
2581+
let search_paths = SearchPaths::default();
25852582
// All static and dynamic native library dependencies are linked to the local crate.
25862583
let link_static = true;
25872584
let link_dynamic = true;
@@ -2623,7 +2620,7 @@ fn add_upstream_rust_crates<'a>(
26232620
.find(|(ty, _)| *ty == crate_type)
26242621
.expect("failed to find crate type in dependency format list");
26252622

2626-
let search_paths = OnceCell::new();
2623+
let search_paths = SearchPaths::default();
26272624
for &cnum in &codegen_results.crate_info.used_crates {
26282625
// We may not pass all crates through to the linker. Some crates may appear statically in
26292626
// an existing dylib, meaning we'll pick up all the symbols from the dylib.
@@ -2698,7 +2695,7 @@ fn add_upstream_native_libraries(
26982695
tmpdir: &Path,
26992696
link_output_kind: LinkOutputKind,
27002697
) {
2701-
let search_path = OnceCell::new();
2698+
let search_paths = SearchPaths::default();
27022699
for &cnum in &codegen_results.crate_info.used_crates {
27032700
// Static libraries are not linked here, they are linked in `add_upstream_rust_crates`.
27042701
// FIXME: Merge this function to `add_upstream_rust_crates` so that all native libraries
@@ -2720,7 +2717,7 @@ fn add_upstream_native_libraries(
27202717
archive_builder_builder,
27212718
codegen_results,
27222719
tmpdir,
2723-
&search_path,
2720+
&search_paths,
27242721
&Default::default(),
27252722
cnum,
27262723
link_static,
@@ -2791,7 +2788,7 @@ fn add_static_crate<'a>(
27912788
} else {
27922789
fix_windows_verbatim_for_gcc(path)
27932790
};
2794-
cmd.link_rlib(&rlib_path);
2791+
cmd.link_staticlib_by_path(&rlib_path, false);
27952792
};
27962793

27972794
if !are_upstream_rust_objects_already_included(sess)
@@ -2859,13 +2856,20 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
28592856
// Just need to tell the linker about where the library lives and
28602857
// what its name is
28612858
let parent = cratepath.parent();
2859+
// When producing a dll, the MSVC linker may not actually emit a
2860+
// `foo.lib` file if the dll doesn't actually export any symbols, so we
2861+
// check to see if the file is there and just omit linking to it if it's
2862+
// not present.
2863+
if sess.target.is_like_msvc && !cratepath.with_extension("dll.lib").exists() {
2864+
return;
2865+
}
28622866
if let Some(dir) = parent {
28632867
cmd.include_path(&rehome_sysroot_lib_dir(sess, dir));
28642868
}
28652869
let stem = cratepath.file_stem().unwrap().to_str().unwrap();
28662870
// Convert library file-stem into a cc -l argument.
28672871
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
2868-
cmd.link_rust_dylib(&stem[prefix..], parent.unwrap_or_else(|| Path::new("")));
2872+
cmd.link_dylib_by_name(&stem[prefix..], false, true);
28692873
}
28702874

28712875
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {

0 commit comments

Comments
 (0)