summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src
AgeCommit message (Collapse)AuthorLines
2023-12-20coverage: Check for `async fn` explicitly, without needing a heuristicZalathar-12/+11
The old code used a heuristic to detect async functions and adjust their coverage spans to produce better output. But there's no need to resort to a heuristic when we can just check whether the current function is actually an `async fn`.
2023-12-20coverage: Pass around `&ExtractedHirInfo` instead of individual fieldsZalathar-29/+16
This reduces the risk of mixing up `fn_source_span` and `body_span`, and makes it easier to pass along additional fields as needed.
2023-12-18Auto merge of #119069 - matthiaskrgr:rollup-xxk4m30, r=matthiaskrgrbors-4/+15
Rollup of 5 pull requests Successful merges: - #118852 (coverage: Skip instrumenting a function if no spans were extracted from MIR) - #118905 ([AIX] Fix XCOFF metadata) - #118967 (Add better ICE messages for some undescriptive panics) - #119051 (Replace `FileAllocationInfo` with `FileEndOfFileInfo`) - #119059 (Deny `~const` trait bounds in inherent impl headers) r? `@ghost` `@rustbot` modify labels: rollup
2023-12-18Rollup merge of #118852 - Zalathar:no-spans, r=cjgillotMatthias Krüger-4/+15
coverage: Skip instrumenting a function if no spans were extracted from MIR The immediate symptoms of #118643 were fixed by #118666, but some users reported that their builds now encounter another coverage-related ICE: ``` error: internal compiler error: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs:98:17: A used function should have had coverage mapping data but did not: (...) ``` I was able to reproduce at least one cause of this error: if no relevant spans could be extracted from a function, but the function contains `CoverageKind::SpanMarker` statements, then codegen still thinks the function is instrumented and complains about the fact that it has no coverage spans. This PR prevents that from happening in two ways: - If we didn't extract any relevant spans from MIR, skip instrumenting the entire function and don't create a `FunctionCoverateInfo` for it. - If coverage codegen sees a `CoverageKind::SpanMarker` statement, skip it early and avoid creating `func_coverage`. --- Fixes #118850.
2023-12-18Rename many `DiagCtxt` and `EarlyDiagCtxt` locals.Nicholas Nethercote-2/+2
2023-12-18Rename many `DiagCtxt` arguments.Nicholas Nethercote-6/+3
2023-12-18Rename `DiagnosticBuilder::handler` as `DiagnosticBuilder::dcx`.Nicholas Nethercote-1/+1
2023-12-18Rename `Handler` as `DiagCtxt`.Nicholas Nethercote-3/+6
2023-12-17Avoid overflow in GVN constant indexing.Camille GILLOT-5/+3
2023-12-15Rollup merge of #119004 - matthiaskrgr:conv, r=compiler-errorsJubilee-1/+1
NFC don't convert types to identical types
2023-12-16Simplify lint decorator derive tooMichael Goulet-17/+3
2023-12-16coverage: Skip instrumenting a function if no spans were extractedZalathar-4/+15
2023-12-15NFC don't convert types to identical typesMatthias Krüger-1/+1
2023-12-15Auto merge of #118966 - matthiaskrgr:rollup-sdvjwy6, r=matthiaskrgrbors-70/+77
Rollup of 3 pull requests Successful merges: - #116888 (Add discussion that concurrent access to the environment is unsafe) - #118888 (Uplift `TypeAndMut` and `ClosureKind` to `rustc_type_ir`) - #118929 (coverage: Tidy up early parts of the instrumentor pass) r? `@ghost` `@rustbot` modify labels: rollup
2023-12-15Rollup merge of #118929 - Zalathar:look-hir, r=cjgillotMatthias Krüger-70/+77
coverage: Tidy up early parts of the instrumentor pass This is extracted from #118237, which needed to be manually rebased anyway. Unlike that PR, this one only affects the coverage instrumentor, and doesn't attempt to move any code into the MIR builder. That can be left to a future version of #118305, which can still benefit from these improvements. So this is now mostly a refactoring of some internal parts of the instrumentor.
2023-12-15Auto merge of #118770 - saethlin:fix-inline-never-uses, r=nnethercotebors-7/+12
Fix cases where std accidentally relied on inline(never) This PR increases the power of `-Zcross-crate-inline-threshold=always` so that it applies through `#[inline(never)]`. Note that though this is called "cross-crate-inlining" in this case especially it is _just_ lazy per-CGU codegen. The MIR inliner and LLVM still respect the attribute as much as they ever have. Trying to bootstrap with the new `-Zcross-crate-inline-threshold=always` change revealed two bugs: We have special intrinsics `assert_inhabited`, `assert_zero_valid`, and `assert_mem_uniniitalized_valid` which codegen backends will lower to nothing or a call to `panic_nounwind`. Since we may not have any call to `panic_nounwind` in MIR but emit one anyway, we need to specially tell `MirUsedCollector` about this situation. `#[lang = "start"]` is special-cased already so that `MirUsedCollector` will collect it, but then when we make it cross-crate-inlinable it is only assigned to a CGU based on whether `MirUsedCollector` saw a call to it, which of course we didn't. --- I started looking into this because https://github.com/rust-lang/rust/pull/118683 revealed a case where we were accidentally relying on a function being `#[inline(never)]`, and cranking up cross-crate-inlinability seems like a way to find other situations like that. r? `@nnethercote` because I don't like what I'm doing to the CGU partitioning code here but I can't come up with something much better
2023-12-14Rollup merge of #118933 - nnethercote:cleanup-errors-even-more, ↵Jubilee-1/+1
r=compiler-errors Cleanup errors handlers even more A sequel to #118587. r? `@compiler-errors`
2023-12-15coverage: Check that the function signature span precedes the bodyZalathar-2/+4
This will normally be true, but in cases where it's not true we're better off not making any assumptions about the signature.
2023-12-15coverage: Compare span source files without involving `Lrc<SourceFile>`Zalathar-11/+14
If we want to know whether two byte positions are in the same file, we don't need to clone and compare `Lrc<SourceFile>`; we can just get their indices and compare those instead.
2023-12-15coverage: Inline and simplify `fn_sig_and_body`Zalathar-14/+9
2023-12-15coverage: Use `LocalDefId` in `extract_hir_info`Zalathar-10/+8
2023-12-15coverage: Extract helper for getting HIR info for coverageZalathar-28/+35
2023-12-15coverage: Don't bother storing the source file in `Instrumentor`Zalathar-4/+3
We can just as easily look it up again from the source map and body span when needed.
2023-12-15coverage: Extract `is_eligible_for_coverage`Zalathar-14/+25
2023-12-15coverage: Simplify parts of `InstrumentCoverage::run_pass`Zalathar-8/+6
Changes in this patch: - Extract local variable `def_id` - Check `is_fn_like` without retrieving HIR - Inline some locals that are used once and aren't needed for clarity
2023-12-15coverage: Assert that the instrumentor never sees promoted MIRZalathar-9/+3
2023-12-14Fix cases where std accidentally relied on inline(never)Ben Kimock-7/+12
2023-12-14Avoid `struct_diagnostic` where possible.Nicholas Nethercote-1/+1
It's necessary for `derive(Diagnostic)`, but is best avoided elsewhere because there are clearer alternatives. This required adding `Handler::struct_almost_fatal`.
2023-12-13Auto merge of #118500 - ZetaNumbers:tcx_hir_refactor, r=petrochenkovbors-1/+1
Move some methods from `tcx.hir()` to `tcx` https://github.com/rust-lang/rust/pull/118256#issuecomment-1826442834 Renamed: - find -> opt_hir_node - get -> hir_node - find_by_def_id -> opt_hir_node_by_def_id - get_by_def_id -> hir_node_by_def_id
2023-12-12clippy::complexity fixesMatthias Krüger-1/+1
filter_map_identity needless_bool search_is_some unit_arg map_identity needless_question_mark derivable_impls
2023-12-12Move some methods from `tcx.hir()` to `tcx`zetanumbers-1/+1
Renamings: - find -> opt_hir_node - get -> hir_node - find_by_def_id -> opt_hir_node_by_def_id - get_by_def_id -> hir_node_by_def_id Fix rebase changes using removed methods Use `tcx.hir_node_by_def_id()` whenever possible in compiler Fix clippy errors Fix compiler Apply suggestions from code review Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com> Add FIXME for `tcx.hir()` returned type about its removal Simplify with with `tcx.hir_node_by_def_id`
2023-12-11Remove redundant special case for resume argumentTomasz Miąsko-7/+0
The special case is subsumed by the check for always live locals that follows it.
2023-12-11End locals' live range before suspending coroutineTomasz Miąsko-1/+15
State transforms retains storage statements for locals that are not stored inside a coroutine. It ensures those locals are live when resuming by inserting StorageLive as appropriate. It forgot to end the storage of those locals when suspending, which is fixed here. While the end of live range is implicit when executing return, it is nevertheless useful for inliner which would otherwise extend the live range beyond return.
2023-12-10remove redundant importssurechen-48/+6
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-12-09Rollup merge of #118764 - compiler-errors:fused-async-iterator, r=eholkJubilee-11/+33
Make async generators fused by default I actually changed my mind about this since the implementation PR landed. I think it's beneficial for `async gen` blocks to be "fused" by default -- i.e., for them to repeatedly return `Poll::Ready(None)` -- rather than panic. We have [`FusedStream`](https://docs.rs/futures/latest/futures/stream/trait.FusedStream.html) in futures-rs to represent streams with this capability already anyways. r? eholk cc ```@rust-lang/wg-async,``` would like to know if anyone else has opinions about this.
2023-12-09Rollup merge of #118666 - Zalathar:body-closure, r=cjgillotJubilee-31/+26
coverage: Simplify the heuristic for ignoring `async fn` return spans The code for extracting coverage spans from MIR has a special heuristic for dealing with `async fn`, so that the function's closing brace does not have a confusing double count. The code implementing that heuristic is currently mixed in with the code for flushing remaining spans after the main refinement loop, making the refinement code harder to understand. We can solve that by hoisting the heuristic to an earlier stage, after the spans have been extracted and sorted but before they have been processed by the refinement loop. The coverage tests verify that the heuristic is still effective, so coverage mappings/reports for `async fn` have not changed. --- This PR also has the side-effect of fixing the `None some_prev` panic that started appearing after #118525. The old code assumed that `prev` would always be present after the refinement loop. That was only true if the list of collected spans was non-empty, but prior to #118525 that didn't seem to come up in practice. After that change, the list of collected spans could be empty in some specific circumstances, leading to panics. The new code uses an `if let` to inspect `prev`, which correctly does nothing if there is no span present.
2023-12-09Rollup merge of #118198 - Zalathar:if-not, r=cjgillotJubilee-3/+12
coverage: Use `SpanMarker` to improve coverage spans for `if !` expressions Coverage instrumentation works by extracting source code spans from MIR. However, some kinds of syntax are effectively erased during MIR building, so their spans don't necessarily exist anywhere in MIR, making them invisible to the coverage instrumentor (unless we resort to various heuristics and hacks to recover them). This PR introduces `CoverageKind::SpanMarker`, which is a new variant of `StatementKind::Coverage`. Its sole purpose is to represent spans that would otherwise not appear in MIR, so that the coverage instrumentor can extract them. When coverage is enabled, the MIR builder can insert these dummy statements as needed, to improve the accuracy of spans used by coverage mappings. Fixes #115468. --- ```@rustbot``` label +A-code-coverage
2023-12-08Make async generators fused by defaultMichael Goulet-11/+33
2023-12-08Implement `async gen` blocksMichael Goulet-2/+28
2023-12-08Rework coroutine transform to be more flexible in preparation for async ↵Michael Goulet-80/+123
generators
2023-12-08coverage: Add `#[track_caller]` to the span generator's unwrap methodsZalathar-14/+12
This should make it easier to investigate unwrap failures in bug reports.
2023-12-08coverage: Simplify the heuristic for ignoring `async fn` return spansZalathar-17/+14
2023-12-08coverage: Add `CoverageKind::SpanMarker` for including extra spans in MIRZalathar-3/+12
There are cases where coverage instrumentation wants to show a span for some syntax element, but there is no MIR node that naturally carries that span, so the instrumentor can't see it. MIR building can now use this new kind of coverage statement to deliberately include those spans in MIR, attached to a dummy statement that has no other effect.
2023-12-08Rollup merge of #118695 - Zalathar:push-refined, r=davidtwcoMatthias Krüger-50/+37
coverage: Merge refined spans in a separate final pass Pulling this merge step out of `push_refined_span` and into a separate pass lets us push directly to `refined_spans` instead of calling a helper method. Because the compiler can now see partial borrows of `refined_spans`, we can remove some extra code that was jumping through hoops to satisfy the borrow checker. --- ``@rustbot`` label +A-code-coverage
2023-12-08Rollup merge of #118690 - Zalathar:test-macros, r=cjgillotMatthias Krüger-70/+33
coverage: Avoid unnecessary macros in unit tests These macros don't provide enough value to justify their complexity, when they can just as easily be functions instead. --- `@rustbot` label +A-code-coverage
2023-12-07Auto merge of #118324 - RalfJung:ctfe-read-only-pointers, r=saethlinbors-8/+10
compile-time evaluation: detect writes through immutable pointers This has two motivations: - it unblocks https://github.com/rust-lang/rust/pull/116745 (and therefore takes a big step towards `const_mut_refs` stabilization), because we can now detect if the memory that we find in `const` can be interned as "immutable" - it would detect the UB that was uncovered in https://github.com/rust-lang/rust/pull/117905, which was caused by accidental stabilization of `copy` functions in `const` that can only be called with UB When UB is detected, we emit a future-compat warn-by-default lint. This is not a breaking change, so completely in line with [the const-UB RFC](https://rust-lang.github.io/rfcs/3016-const-ub.html), meaning we don't need t-lang FCP here. I made the lint immediately show up for dependencies since it is nearly impossible to even trigger this lint without `const_mut_refs` -- the accidentally stabilized `copy` functions are the only way this can happen, so the crates that popped up in #117905 are the only causes of such UB (in the code that crater covers), and the three cases of UB that we know about have all been fixed in their respective crates already. The way this is implemented is by making use of the fact that our interpreter is already generic over the notion of provenance. For CTFE we now use the new `CtfeProvenance` type which is conceptually an `AllocId` plus a boolean `immutable` flag (but packed for a more efficient representation). This means we can mark a pointer as immutable when it is created as a shared reference. The flag will be propagated to all pointers derived from this one. We can then check the immutable flag on each write to reject writes through immutable pointers. I just hope perf works out.
2023-12-07compile-time evaluation: emit a lint when a write through an immutable ↵Ralf Jung-2/+4
pointer occurs
2023-12-07ctfe interpreter: extend provenance so that it can track whether a pointer ↵Ralf Jung-6/+6
is immutable
2023-12-07coverage: Simplify code that pushes to `refined_spans`Zalathar-26/+14
2023-12-07coverage: Inline `push_refined_span`Zalathar-13/+9