Skip to content

Commit 26b4557

Browse files
committed
Auto merge of #91418 - matthiaskrgr:rollup-vn9f9w3, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #87160 (When recovering from a `:` in a pattern, use adequate AST pattern) - #90985 (Use `get_diagnostic_name` more) - #91087 (Remove all migrate.nll.stderr files) - #91207 (Add support for LLVM coverage mapping format versions 5 and 6) - #91298 (Improve error message for `E0659` if the source is not available) - #91346 (Add `Option::inspect` and `Result::{inspect, inspect_err}`) - #91404 (Fix bad `NodeId` limit checking.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2446a21 + 4f252f1 commit 26b4557

File tree

53 files changed

+617
-458
lines changed

Some content is hidden

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

53 files changed

+617
-458
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,13 @@ impl BorrowedContentSource<'tcx> {
738738
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
739739
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
740740
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
741-
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() {
742-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
743-
"an `Rc`".to_string()
744-
}
745-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
746-
"an `Arc`".to_string()
747-
}
748-
_ => format!("dereference of `{}`", ty),
749-
},
741+
BorrowedContentSource::OverloadedDeref(ty) => ty
742+
.ty_adt_def()
743+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
744+
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
745+
_ => None,
746+
})
747+
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
750748
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
751749
}
752750
}
@@ -770,15 +768,13 @@ impl BorrowedContentSource<'tcx> {
770768
BorrowedContentSource::DerefMutableRef => {
771769
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
772770
}
773-
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() {
774-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
775-
"an `Rc`".to_string()
776-
}
777-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
778-
"an `Arc`".to_string()
779-
}
780-
_ => format!("a dereference of `{}`", ty),
781-
},
771+
BorrowedContentSource::OverloadedDeref(ty) => ty
772+
.ty_adt_def()
773+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
774+
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
775+
_ => None,
776+
})
777+
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
782778
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
783779
}
784780
}
@@ -960,8 +956,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
960956
_ => None,
961957
});
962958
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
963-
tcx.is_diagnostic_item(sym::Option, def_id)
964-
|| tcx.is_diagnostic_item(sym::Result, def_id)
959+
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
965960
});
966961
FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result }
967962
});

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
99
use rustc_hir::def_id::{DefId, DefIdSet};
1010
use rustc_llvm::RustString;
1111
use rustc_middle::mir::coverage::CodeRegion;
12+
use rustc_middle::ty::TyCtxt;
1213
use rustc_span::Symbol;
1314

1415
use std::ffi::CString;
@@ -17,10 +18,11 @@ use tracing::debug;
1718

1819
/// Generates and exports the Coverage Map.
1920
///
20-
/// This Coverage Map complies with Coverage Mapping Format version 4 (zero-based encoded as 3),
21-
/// as defined at [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format)
22-
/// and published in Rust's November 2020 fork of LLVM. This version is supported by the LLVM
23-
/// coverage tools (`llvm-profdata` and `llvm-cov`) bundled with Rust's fork of LLVM.
21+
/// Rust Coverage Map generation supports LLVM Coverage Mapping Format versions
22+
/// 5 (LLVM 12, only) and 6 (zero-based encoded as 4 and 5, respectively), as defined at
23+
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
24+
/// These versions are supported by the LLVM coverage tools (`llvm-profdata` and `llvm-cov`)
25+
/// bundled with Rust's fork of LLVM.
2426
///
2527
/// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
2628
/// the same version. Clang's implementation of Coverage Map generation was referenced when
@@ -30,11 +32,12 @@ use tracing::debug;
3032
pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
3133
let tcx = cx.tcx;
3234

33-
// Ensure LLVM supports Coverage Map Version 4 (encoded as a zero-based value: 3).
34-
// If not, the LLVM Version must be less than 11.
35+
// Ensure the installed version of LLVM supports at least Coverage Map
36+
// Version 5 (encoded as a zero-based value: 4), which was introduced with
37+
// LLVM 12.
3538
let version = coverageinfo::mapping_version();
36-
if version != 3 {
37-
tcx.sess.fatal("rustc option `-Z instrument-coverage` requires LLVM 11 or higher.");
39+
if version < 4 {
40+
tcx.sess.fatal("rustc option `-Z instrument-coverage` requires LLVM 12 or higher.");
3841
}
3942

4043
debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
@@ -57,7 +60,7 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
5760
return;
5861
}
5962

60-
let mut mapgen = CoverageMapGenerator::new();
63+
let mut mapgen = CoverageMapGenerator::new(tcx, version);
6164

6265
// Encode coverage mappings and generate function records
6366
let mut function_data = Vec::new();
@@ -112,8 +115,26 @@ struct CoverageMapGenerator {
112115
}
113116

114117
impl CoverageMapGenerator {
115-
fn new() -> Self {
116-
Self { filenames: FxIndexSet::default() }
118+
fn new(tcx: TyCtxt<'_>, version: u32) -> Self {
119+
let mut filenames = FxIndexSet::default();
120+
if version >= 5 {
121+
// LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
122+
// requires setting the first filename to the compilation directory.
123+
// Since rustc generates coverage maps with relative paths, the
124+
// compilation directory can be combined with the the relative paths
125+
// to get absolute paths, if needed.
126+
let working_dir = tcx
127+
.sess
128+
.opts
129+
.working_dir
130+
.remapped_path_if_available()
131+
.to_string_lossy()
132+
.to_string();
133+
let c_filename =
134+
CString::new(working_dir).expect("null error converting filename to C string");
135+
filenames.insert(c_filename);
136+
}
137+
Self { filenames }
117138
}
118139

119140
/// Using the `expressions` and `counter_regions` collected for the current function, generate

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_v
685685
pub mod coverageinfo {
686686
use super::coverage_map;
687687

688-
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L206-L222)
688+
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L209-L230)
689689
#[derive(Copy, Clone, Debug)]
690690
#[repr(C)]
691691
pub enum RegionKind {
@@ -704,11 +704,16 @@ pub mod coverageinfo {
704704
/// A GapRegion is like a CodeRegion, but its count is only set as the
705705
/// line execution count when its the only region in the line.
706706
GapRegion = 3,
707+
708+
/// A BranchRegion represents leaf-level boolean expressions and is
709+
/// associated with two counters, each representing the number of times the
710+
/// expression evaluates to true or false.
711+
BranchRegion = 4,
707712
}
708713

709714
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
710715
/// coverage map, in accordance with the
711-
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
716+
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
712717
/// The struct composes fields representing the `Counter` type and value(s) (injected counter
713718
/// ID, or expression type and operands), the source file (an indirect index into a "filenames
714719
/// array", encoded separately), and source location (start and end positions of the represented
@@ -721,6 +726,10 @@ pub mod coverageinfo {
721726
/// The counter type and type-dependent counter data, if any.
722727
counter: coverage_map::Counter,
723728

729+
/// If the `RegionKind` is a `BranchRegion`, this represents the counter
730+
/// for the false branch of the region.
731+
false_counter: coverage_map::Counter,
732+
724733
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
725734
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
726735
/// that, in turn, are used to look up the filename for this region.
@@ -758,6 +767,7 @@ pub mod coverageinfo {
758767
) -> Self {
759768
Self {
760769
counter,
770+
false_counter: coverage_map::Counter::zero(),
761771
file_id,
762772
expanded_file_id: 0,
763773
start_line,
@@ -768,6 +778,31 @@ pub mod coverageinfo {
768778
}
769779
}
770780

781+
// This function might be used in the future; the LLVM API is still evolving, as is coverage
782+
// support.
783+
#[allow(dead_code)]
784+
crate fn branch_region(
785+
counter: coverage_map::Counter,
786+
false_counter: coverage_map::Counter,
787+
file_id: u32,
788+
start_line: u32,
789+
start_col: u32,
790+
end_line: u32,
791+
end_col: u32,
792+
) -> Self {
793+
Self {
794+
counter,
795+
false_counter,
796+
file_id,
797+
expanded_file_id: 0,
798+
start_line,
799+
start_col,
800+
end_line,
801+
end_col,
802+
kind: RegionKind::BranchRegion,
803+
}
804+
}
805+
771806
// This function might be used in the future; the LLVM API is still evolving, as is coverage
772807
// support.
773808
#[allow(dead_code)]
@@ -781,6 +816,7 @@ pub mod coverageinfo {
781816
) -> Self {
782817
Self {
783818
counter: coverage_map::Counter::zero(),
819+
false_counter: coverage_map::Counter::zero(),
784820
file_id,
785821
expanded_file_id,
786822
start_line,
@@ -803,6 +839,7 @@ pub mod coverageinfo {
803839
) -> Self {
804840
Self {
805841
counter: coverage_map::Counter::zero(),
842+
false_counter: coverage_map::Counter::zero(),
806843
file_id,
807844
expanded_file_id: 0,
808845
start_line,
@@ -826,6 +863,7 @@ pub mod coverageinfo {
826863
) -> Self {
827864
Self {
828865
counter,
866+
false_counter: coverage_map::Counter::zero(),
829867
file_id,
830868
expanded_file_id: 0,
831869
start_line,

compiler/rustc_codegen_ssa/src/coverageinfo/ffi.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::mir::coverage::{CounterValueReference, MappedExpressionIndex};
22

3-
/// Aligns with [llvm::coverage::Counter::CounterKind](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L206-L222)
3+
/// Aligns with [llvm::coverage::Counter::CounterKind](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L95)
44
#[derive(Copy, Clone, Debug)]
55
#[repr(C)]
66
pub enum CounterKind {
@@ -17,7 +17,7 @@ pub enum CounterKind {
1717
/// `instrprof.increment()`)
1818
/// * For `CounterKind::Expression`, `id` is the index into the coverage map's array of
1919
/// counter expressions.
20-
/// Aligns with [llvm::coverage::Counter](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L99-L100)
20+
/// Aligns with [llvm::coverage::Counter](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L102-L103)
2121
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
2222
#[derive(Copy, Clone, Debug)]
2323
#[repr(C)]
@@ -59,15 +59,15 @@ impl Counter {
5959
}
6060
}
6161

62-
/// Aligns with [llvm::coverage::CounterExpression::ExprKind](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L147)
62+
/// Aligns with [llvm::coverage::CounterExpression::ExprKind](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L150)
6363
#[derive(Copy, Clone, Debug)]
6464
#[repr(C)]
6565
pub enum ExprKind {
6666
Subtract = 0,
6767
Add = 1,
6868
}
6969

70-
/// Aligns with [llvm::coverage::CounterExpression](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L148-L149)
70+
/// Aligns with [llvm::coverage::CounterExpression](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L151-L152)
7171
/// Important: The Rust struct layout (order and types of fields) must match its C++
7272
/// counterpart.
7373
#[derive(Copy, Clone, Debug)]

compiler/rustc_lint/src/enum_intrinsics_non_enums.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,14 @@ fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, sp
9191

9292
impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums {
9393
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
94-
if let hir::ExprKind::Call(ref func, ref args) = expr.kind {
95-
if let hir::ExprKind::Path(ref qpath) = func.kind {
96-
if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() {
97-
if cx.tcx.is_diagnostic_item(sym::mem_discriminant, def_id) {
98-
enforce_mem_discriminant(cx, func, expr.span, args[0].span);
99-
} else if cx.tcx.is_diagnostic_item(sym::mem_variant_count, def_id) {
100-
enforce_mem_variant_count(cx, func, expr.span);
101-
}
102-
}
103-
}
94+
let hir::ExprKind::Call(func, args) = &expr.kind else { return };
95+
let hir::ExprKind::Path(qpath) = &func.kind else { return };
96+
let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() else { return };
97+
let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return };
98+
match name {
99+
sym::mem_discriminant => enforce_mem_discriminant(cx, func, expr.span, args[0].span),
100+
sym::mem_variant_count => enforce_mem_variant_count(cx, func, expr.span),
101+
_ => {}
104102
}
105103
}
106104
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![cfg_attr(bootstrap, feature(format_args_capture))]
3434
#![feature(iter_order_by)]
3535
#![feature(iter_zip)]
36+
#![feature(let_else)]
3637
#![feature(never_type)]
3738
#![feature(nll)]
3839
#![feature(control_flow_enum)]

compiler/rustc_lint/src/non_fmt_panic.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,21 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
309309
// Unwrap more levels of macro expansion, as panic_2015!()
310310
// was likely expanded from panic!() and possibly from
311311
// [debug_]assert!().
312-
for &i in
313-
&[sym::std_panic_macro, sym::core_panic_macro, sym::assert_macro, sym::debug_assert_macro]
314-
{
312+
loop {
315313
let parent = expn.call_site.ctxt().outer_expn_data();
316-
if parent.macro_def_id.map_or(false, |id| cx.tcx.is_diagnostic_item(i, id)) {
317-
expn = parent;
318-
panic_macro = i;
314+
let Some(id) = parent.macro_def_id else { break };
315+
let Some(name) = cx.tcx.get_diagnostic_name(id) else { break };
316+
if !matches!(
317+
name,
318+
sym::core_panic_macro
319+
| sym::std_panic_macro
320+
| sym::assert_macro
321+
| sym::debug_assert_macro
322+
) {
323+
break;
319324
}
325+
expn = parent;
326+
panic_macro = name;
320327
}
321328

322329
let macro_symbol =

0 commit comments

Comments
 (0)