Skip to content

Commit 017a6f7

Browse files
committed
Auto merge of #145062 - tgross35:rollup-1wm2fv2, r=tgross35
Rollup of 6 pull requests Successful merges: - #144400 (`tests/ui/issues/`: The Issues Strike Back [3/N]) - #144678 (Make no_mangle on foreign items explicit instead of implicit) - #145045 (doc(library): Fix Markdown in `Iterator::by_ref`) - #145046 (Fix doc comment of File::try_lock and File::try_lock_shared) - #145047 (move `type_check` out of `compute_regions`) - #145053 (Add a lot of NLL `known-bug` tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2fd855f + 330a4e3 commit 017a6f7

File tree

96 files changed

+1255
-169
lines changed

Some content is hidden

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

96 files changed

+1255
-169
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::borrow::Cow;
1919
use std::cell::{OnceCell, RefCell};
2020
use std::marker::PhantomData;
2121
use std::ops::{ControlFlow, Deref};
22+
use std::rc::Rc;
2223

2324
use borrow_set::LocalsStateAtExit;
2425
use root_cx::BorrowCheckRootCtxt;
@@ -44,6 +45,7 @@ use rustc_mir_dataflow::impls::{EverInitializedPlaces, MaybeUninitializedPlaces}
4445
use rustc_mir_dataflow::move_paths::{
4546
InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,
4647
};
48+
use rustc_mir_dataflow::points::DenseLocationMap;
4749
use rustc_mir_dataflow::{Analysis, Results, ResultsVisitor, visit_results};
4850
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
4951
use rustc_span::{ErrorGuaranteed, Span, Symbol};
@@ -60,11 +62,14 @@ use crate::path_utils::*;
6062
use crate::place_ext::PlaceExt;
6163
use crate::places_conflict::{PlaceConflictBias, places_conflict};
6264
use crate::polonius::PoloniusDiagnosticsContext;
63-
use crate::polonius::legacy::{PoloniusLocationTable, PoloniusOutput};
65+
use crate::polonius::legacy::{
66+
PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
67+
};
6468
use crate::prefixes::PrefixSet;
6569
use crate::region_infer::RegionInferenceContext;
6670
use crate::renumber::RegionCtxt;
6771
use crate::session_diagnostics::VarNeedNotMut;
72+
use crate::type_check::MirTypeckResults;
6873

6974
mod borrow_set;
7075
mod borrowck_errors;
@@ -321,7 +326,34 @@ fn do_mir_borrowck<'tcx>(
321326
let locals_are_invalidated_at_exit = tcx.hir_body_owner_kind(def).is_fn_or_closure();
322327
let borrow_set = BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data);
323328

324-
// Compute non-lexical lifetimes.
329+
let location_map = Rc::new(DenseLocationMap::new(body));
330+
331+
let polonius_input = root_cx.consumer.as_ref().map_or(false, |c| c.polonius_input())
332+
|| infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();
333+
let mut polonius_facts =
334+
(polonius_input || PoloniusFacts::enabled(infcx.tcx)).then_some(PoloniusFacts::default());
335+
336+
// Run the MIR type-checker.
337+
let MirTypeckResults {
338+
constraints,
339+
universal_region_relations,
340+
opaque_type_values,
341+
polonius_context,
342+
} = type_check::type_check(
343+
root_cx,
344+
&infcx,
345+
body,
346+
&promoted,
347+
universal_regions,
348+
&location_table,
349+
&borrow_set,
350+
&mut polonius_facts,
351+
&move_data,
352+
Rc::clone(&location_map),
353+
);
354+
355+
// Compute non-lexical lifetimes using the constraints computed
356+
// by typechecking the MIR body.
325357
let nll::NllOutput {
326358
regioncx,
327359
polonius_input,
@@ -332,14 +364,19 @@ fn do_mir_borrowck<'tcx>(
332364
} = nll::compute_regions(
333365
root_cx,
334366
&infcx,
335-
universal_regions,
336367
body,
337-
&promoted,
338368
&location_table,
339369
&move_data,
340370
&borrow_set,
371+
location_map,
372+
universal_region_relations,
373+
constraints,
374+
polonius_facts,
375+
polonius_context,
341376
);
342377

378+
regioncx.infer_opaque_types(root_cx, &infcx, opaque_type_values);
379+
343380
// Dump MIR results into a file, if that is enabled. This lets us
344381
// write unit-tests, as well as helping with debugging.
345382
nll::dump_nll_mir(&infcx, body, &regioncx, &opt_closure_req, &borrow_set);

compiler/rustc_borrowck/src/nll.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::path::PathBuf;
55
use std::rc::Rc;
66
use std::str::FromStr;
77

8-
use polonius_engine::{Algorithm, Output};
8+
use polonius_engine::{Algorithm, AllFacts, Output};
9+
use rustc_data_structures::frozen::Frozen;
910
use rustc_index::IndexSlice;
1011
use rustc_middle::mir::pretty::{PrettyPrintMirOptions, dump_mir_with_options};
1112
use rustc_middle::mir::{Body, PassWhere, Promoted, create_dump_file, dump_enabled, dump_mir};
@@ -18,14 +19,16 @@ use rustc_span::sym;
1819
use tracing::{debug, instrument};
1920

2021
use crate::borrow_set::BorrowSet;
22+
use crate::consumers::RustcFacts;
2123
use crate::diagnostics::RegionErrors;
2224
use crate::handle_placeholders::compute_sccs_applying_placeholder_outlives_constraints;
23-
use crate::polonius::PoloniusDiagnosticsContext;
2425
use crate::polonius::legacy::{
2526
PoloniusFacts, PoloniusFactsExt, PoloniusLocationTable, PoloniusOutput,
2627
};
28+
use crate::polonius::{PoloniusContext, PoloniusDiagnosticsContext};
2729
use crate::region_infer::RegionInferenceContext;
28-
use crate::type_check::{self, MirTypeckResults};
30+
use crate::type_check::MirTypeckRegionConstraints;
31+
use crate::type_check::free_region_relations::UniversalRegionRelations;
2932
use crate::universal_regions::UniversalRegions;
3033
use crate::{
3134
BorrowCheckRootCtxt, BorrowckInferCtxt, ClosureOutlivesSubject, ClosureRegionRequirements,
@@ -76,41 +79,18 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
7679
pub(crate) fn compute_regions<'tcx>(
7780
root_cx: &mut BorrowCheckRootCtxt<'tcx>,
7881
infcx: &BorrowckInferCtxt<'tcx>,
79-
universal_regions: UniversalRegions<'tcx>,
8082
body: &Body<'tcx>,
81-
promoted: &IndexSlice<Promoted, Body<'tcx>>,
8283
location_table: &PoloniusLocationTable,
8384
move_data: &MoveData<'tcx>,
8485
borrow_set: &BorrowSet<'tcx>,
86+
location_map: Rc<DenseLocationMap>,
87+
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
88+
constraints: MirTypeckRegionConstraints<'tcx>,
89+
mut polonius_facts: Option<AllFacts<RustcFacts>>,
90+
polonius_context: Option<PoloniusContext>,
8591
) -> NllOutput<'tcx> {
86-
let is_polonius_legacy_enabled = infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();
87-
let polonius_input = root_cx.consumer.as_ref().map_or(false, |c| c.polonius_input())
88-
|| is_polonius_legacy_enabled;
8992
let polonius_output = root_cx.consumer.as_ref().map_or(false, |c| c.polonius_output())
90-
|| is_polonius_legacy_enabled;
91-
let mut polonius_facts =
92-
(polonius_input || PoloniusFacts::enabled(infcx.tcx)).then_some(PoloniusFacts::default());
93-
94-
let location_map = Rc::new(DenseLocationMap::new(body));
95-
96-
// Run the MIR type-checker.
97-
let MirTypeckResults {
98-
constraints,
99-
universal_region_relations,
100-
opaque_type_values,
101-
polonius_context,
102-
} = type_check::type_check(
103-
root_cx,
104-
infcx,
105-
body,
106-
promoted,
107-
universal_regions,
108-
location_table,
109-
borrow_set,
110-
&mut polonius_facts,
111-
move_data,
112-
Rc::clone(&location_map),
113-
);
93+
|| infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled();
11494

11595
let lowered_constraints = compute_sccs_applying_placeholder_outlives_constraints(
11696
constraints,
@@ -173,8 +153,6 @@ pub(crate) fn compute_regions<'tcx>(
173153
infcx.set_tainted_by_errors(guar);
174154
}
175155

176-
regioncx.infer_opaque_types(root_cx, infcx, opaque_type_values);
177-
178156
NllOutput {
179157
regioncx,
180158
polonius_input: polonius_facts.map(Box::new),

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
586586
// core/std/allocators/etc. For example symbols used to hook up allocation
587587
// are not considered for export
588588
let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
589-
let is_extern = codegen_fn_attrs.contains_extern_indicator();
589+
let is_extern = codegen_fn_attrs.contains_extern_indicator(tcx, sym_def_id);
590590
let std_internal =
591591
codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
592592

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,35 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
443443
if tcx.should_inherit_track_caller(did) {
444444
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
445445
}
446+
447+
// Foreign items by default use no mangling for their symbol name.
448+
if tcx.is_foreign_item(did) {
449+
// There's a few exceptions to this rule though:
450+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
451+
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
452+
// both for exports and imports through foreign items. This is handled further,
453+
// during symbol mangling logic.
454+
} else if codegen_fn_attrs.link_name.is_some() {
455+
// * This can be overridden with the `#[link_name]` attribute
456+
} else if tcx.sess.target.is_like_wasm
457+
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&did.into())
458+
{
459+
// * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
460+
// same-named symbol when imported from different wasm modules will get
461+
// hooked up incorrectly. As a result foreign symbols, on the wasm target,
462+
// with a wasm import module, get mangled. Additionally our codegen will
463+
// deduplicate symbols based purely on the symbol name, but for wasm this
464+
// isn't quite right because the same-named symbol on wasm can come from
465+
// different modules. For these reasons if `#[link(wasm_import_module)]`
466+
// is present we mangle everything on wasm because the demangled form will
467+
// show up in the `wasm-import-name` custom attribute in LLVM IR.
468+
//
469+
// [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
470+
} else {
471+
// if none of the exceptions apply; apply no_mangle
472+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
473+
}
474+
}
446475
}
447476

448477
fn check_result(

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::borrow::Cow;
33
use rustc_abi::Align;
44
use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
55
use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr};
6+
use rustc_hir::def_id::DefId;
67
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
78
use rustc_span::Symbol;
89
use rustc_target::spec::SanitizerSet;
@@ -193,7 +194,11 @@ impl CodegenFnAttrs {
193194
/// * `#[linkage]` is present
194195
///
195196
/// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
196-
pub fn contains_extern_indicator(&self) -> bool {
197+
pub fn contains_extern_indicator(&self, tcx: TyCtxt<'_>, did: DefId) -> bool {
198+
if tcx.is_foreign_item(did) {
199+
return false;
200+
}
201+
197202
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
198203
|| self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
199204
|| self.export_name.is_some()

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'tcx> MonoItem<'tcx> {
151151
// instantiation:
152152
// We emit an unused_attributes lint for this case, which should be kept in sync if possible.
153153
let codegen_fn_attrs = tcx.codegen_instance_attrs(instance.def);
154-
if codegen_fn_attrs.contains_extern_indicator()
154+
if codegen_fn_attrs.contains_extern_indicator(tcx, instance.def.def_id())
155155
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED)
156156
{
157157
return InstantiationMode::GloballyShared { may_conflict: false };

compiler/rustc_mir_transform/src/cross_crate_inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1818
let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
1919
// If this has an extern indicator, then this function is globally shared and thus will not
2020
// generate cgu-internal copies which would make it cross-crate inlinable.
21-
if codegen_fn_attrs.contains_extern_indicator() {
21+
if codegen_fn_attrs.contains_extern_indicator(tcx, def_id.into()) {
2222
return false;
2323
}
2424

compiler/rustc_passes/src/check_attr.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,24 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
561561
match target {
562562
Target::Fn
563563
| Target::Closure
564-
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {}
564+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
565+
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
566+
if let Some(did) = hir_id.as_owner()
567+
&& self.tcx.def_kind(did).has_codegen_attrs()
568+
&& kind != &InlineAttr::Never
569+
{
570+
let attrs = self.tcx.codegen_fn_attrs(did);
571+
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
572+
if attrs.contains_extern_indicator(self.tcx, did.into()) {
573+
self.tcx.emit_node_span_lint(
574+
UNUSED_ATTRIBUTES,
575+
hir_id,
576+
attr_span,
577+
errors::InlineIgnoredForExported {},
578+
);
579+
}
580+
}
581+
}
565582
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
566583
self.tcx.emit_node_span_lint(
567584
UNUSED_ATTRIBUTES,
@@ -588,23 +605,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
588605
self.dcx().emit_err(errors::InlineNotFnOrClosure { attr_span, defn_span });
589606
}
590607
}
591-
592-
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
593-
if let Some(did) = hir_id.as_owner()
594-
&& self.tcx.def_kind(did).has_codegen_attrs()
595-
&& kind != &InlineAttr::Never
596-
{
597-
let attrs = self.tcx.codegen_fn_attrs(did);
598-
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
599-
if attrs.contains_extern_indicator() {
600-
self.tcx.emit_node_span_lint(
601-
UNUSED_ATTRIBUTES,
602-
hir_id,
603-
attr_span,
604-
errors::InlineIgnoredForExported {},
605-
);
606-
}
607-
}
608608
}
609609

610610
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,

compiler/rustc_passes/src/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ fn has_allow_dead_code_or_lang_attr(
701701

702702
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
703703
// forcefully, e.g., for placing it in a specific section.
704-
cg_attrs.contains_extern_indicator()
704+
cg_attrs.contains_extern_indicator(tcx, def_id.into())
705705
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
706706
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
707707
}

compiler/rustc_passes/src/reachable.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> ReachableContext<'tcx> {
183183
} else {
184184
CodegenFnAttrs::EMPTY
185185
};
186-
let is_extern = codegen_attrs.contains_extern_indicator();
186+
let is_extern = codegen_attrs.contains_extern_indicator(self.tcx, search_item.into());
187187
if is_extern {
188188
self.reachable_symbols.insert(search_item);
189189
}
@@ -423,8 +423,9 @@ fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
423423
if !tcx.def_kind(def_id).has_codegen_attrs() {
424424
return false;
425425
}
426+
426427
let codegen_attrs = tcx.codegen_fn_attrs(def_id);
427-
codegen_attrs.contains_extern_indicator()
428+
codegen_attrs.contains_extern_indicator(tcx, def_id.into())
428429
// FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
429430
// `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
430431
// `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.

0 commit comments

Comments
 (0)