Commit graph

591 commits

Author SHA1 Message Date
Nicholas Nethercote
ce2aa97cd6 Move has_self field to hir::AssocKind::Fn.
`hir::AssocItem` currently has a boolean `fn_has_self_parameter` field,
which is misplaced, because it's only relevant for associated fns, not
for associated consts or types. This commit moves it (and renames it) to
the `AssocKind::Fn` variant, where it belongs.

This requires introducing a new C-style enum, `AssocTag`, which is like
`AssocKind` but without the fields. This is because `AssocKind` values
are passed to various functions like `find_by_ident_and_kind` to
indicate what kind of associated item should be searched for, and having
to specify `has_self` isn't relevant there.

New methods:
- Predicates `AssocItem::is_fn` and `AssocItem::is_method`.
- `AssocItem::as_tag` which converts `AssocItem::kind` to `AssocTag`.

Removed `find_by_name_and_kinds`, which is unused.

`AssocItem::descr` can now distinguish between methods and associated
functions, which slightly improves some error messages.
2025-04-14 16:13:04 +10:00
Zalathar
5434242af7 Build UserTypeProjections lazily when visiting bindings 2025-03-16 12:16:09 +11:00
Michael Goulet
19c84c8812 Move codec module back into middle 2025-03-15 06:42:48 +00:00
Michael Goulet
e5a2220327 Fold visit into ty 2025-03-15 06:34:36 +00:00
Thalia Archibald
38fad984c6 compiler: Use size_of from the prelude instead of imported
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the
prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.
2025-03-07 13:37:04 -08:00
Nicholas Nethercote
1eddb158f9 Move impl blocks out of rustc_middle/src/mir/syntax.rs.
As the comment at the top says, this file is not supposed to contain any
code. But some has crept in. This commit moves it out.
2025-02-24 09:16:10 +11:00
Michael Goulet
12e3911d81 Greatly simplify lifetime captures in edition 2024 2025-02-22 22:24:52 +00:00
Matthias Krüger
1f6c75e682
Rollup merge of #137305 - nnethercote:rustc_middle-2, r=lcnr
Tweaks in and around `rustc_middle`

A bunch of tiny improvements I found while working on bigger things.

r? ```@lcnr```
2025-02-21 12:45:25 +01:00
Matthias Krüger
15a0403ecf
Rollup merge of #137204 - nnethercote:clarify-MIR-dialects-and-phases, r=RalfJung
Clarify MIR dialects and phases

I found the existing code and docs hard to understand.

r? `@Zalathar`
2025-02-21 12:45:23 +01:00
Nicholas Nethercote
e03c809402 Remove some unnecessary FIXME comments.
The comments didn't make much sense to me. I asked Matthew Jasper on
Zulip about it and they said:

> I think that at the time I wanted to replace all (or most of) this
> with a reference to the HIR Id of the variable. I'll give this a look
> to see if it's still a reasonable idea, but removing the comments is
> fine.

and then:

> I don't think that changing this to an HirId would be better,
> recovering the information from the HIR seems like too much effort in
> exchange for making the MIR a little smaller.
2025-02-21 07:12:13 +11:00
Nicholas Nethercote
c49e2df668 Put a BlockTailInfo in BlockFrame::TailExpr.
Because it has the same fields, and avoids the need to deconstruct the
latter to construct the former.
2025-02-21 07:12:13 +11:00
Nicholas Nethercote
5d2d11fd5d Rename ClearCrossCrate::assert_crate_local.
As `unwrap_crate_local`, because it follows exactly the standard form of
an `unwrap` function.
2025-02-21 07:12:13 +11:00
Nicholas Nethercote
2f695dc64e Remove unused Body::span_for_ty_context method. 2025-02-21 07:12:13 +11:00
Nicholas Nethercote
c301ba57a6 Fix a typo in a comment. 2025-02-21 07:12:13 +11:00
Zalathar
8bb574fdd3 Don't store a redundant span in user-type projections
This span is already present in the corresponding
`CanonicalUserTypeAnnotation`, and can be retrieved via the annotation's ID.
2025-02-20 20:37:17 +11:00
Zalathar
a64efc72d0 Avoid a useless clone of UserTypeProjection 2025-02-20 20:31:06 +11:00
Nicholas Nethercote
83a7fb61fb Improve how the MIR dialect/phase index is reported.
The only visible change is to the filenames produce by `-Zdump-mir`.
E.g. before and after:
```
h.main.003-000.analysis-post-cleanup.after.mir
h.main.2-2-000.analysis-post-cleanup.after.mir
```
It also fixes a FIXME comment.
2025-02-20 10:28:52 +11:00
Nicholas Nethercote
311e8d3e5d Remove MirVisitable.
The `MirVisitable` trait is just a complicated way to visit either a
statement or a terminator. (And its impl for `Terminator` is unused.) It
has a single use.

This commit removes it, replacing it with an if/else, which is shorter
and simpler.
2025-02-19 19:42:17 +11:00
Matthias Krüger
34535b6078
Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors
Remove `rustc_middle::mir::tcx` module.

This is a really weird module. For example, what does `tcx` in `rustc_middle::mir::tcx::PlaceTy` mean? The answer is "not much".

The top-level module comment says:

> Methods for the various MIR types. These are intended for use after
> building is complete.

Awfully broad for a module that has a handful of impl blocks for some MIR types, none of which really relates to `TyCtxt`. `git blame` indicates the comment is ancient, from 2015, and made sense then.

This module is now vestigial. This commit removes it and moves all the code within into `rustc_middle::mir::statement`. Some specifics:

- `Place`, `PlaceRef`, `Rvalue`, `Operand`, `BorrowKind`: they all have `impl` blocks in both the `tcx` and `statement` modules. The commit merges the former into the latter.

- `BinOp`, `UnOp`: they only have `impl` blocks in `tcx`. The commit moves these into `statement`.

- `PlaceTy`, `RvalueInitializationState`: they are defined in `tcx`. This commit moves them into `statement` *and* makes them available in `mir::*`, like many other MIR types.

r? `@tmandry`
2025-02-19 01:30:13 +01:00
Nicholas Nethercote
5d1551b9c6 Remove rustc_middle::mir::tcx module.
This is a really weird module. For example, what does `tcx` in
`rustc_middle::mir::tcx::PlaceTy` mean? The answer is "not much".

The top-level module comment says:

> Methods for the various MIR types. These are intended for use after
> building is complete.

Awfully broad for a module that has a handful of impl blocks for some
MIR types, none of which really relates to `TyCtxt`. `git blame`
indicates the comment is ancient, from 2015, and made sense then.

This module is now vestigial. This commit removes it and moves all the
code within into `rustc_middle::mir::statement`. Some specifics:

- `Place`, `PlaceRef`, `Rvalue`, `Operand`, `BorrowKind`: they all have `impl`
  blocks in both the `tcx` and `statement` modules. The commit merges
  the former into the latter.

- `BinOp`, `UnOp`: they only have `impl` blocks in `tcx`. The commit
  moves these into `statement`.

- `PlaceTy`, `RvalueInitializationState`: they are defined in `tcx`.
  This commit moves them into `statement` *and* makes them available in
  `mir::*`, like many other MIR types.
2025-02-19 10:26:05 +11:00
Matthias Krüger
10dd016a80
Rollup merge of #137203 - nnethercote:improve-MIR-modification, r=compiler-errors
Improve MIR modification

A few commits that simplify code that manipulates MIR bodies.

r? `@tmiasko`
2025-02-18 18:40:54 +01:00
Nicholas Nethercote
04eeda47ab Inline and replace Statement::replace_nop.
It has a single call site, and doesn't seem worth having as an API
function.
2025-02-18 13:43:43 +11:00
Nicholas Nethercote
69f5e342bf Inline and remove BasicBlockData::retain_statements.
It has a single call site, and the code is clearer this way.
2025-02-18 13:31:08 +11:00
Nicholas Nethercote
627e08c909 Remove BasicBlockData::expand_statements.
The previous commit removed its single use. `MirPatch` is a more
flexible alternative.
2025-02-18 13:13:32 +11:00
Nicholas Nethercote
8403d39dce Add SwitchTargetValue.
This is much clearer than `Option<u128>`.
2025-02-17 09:51:37 +11:00
Nicholas Nethercote
28b75a384e Move MirPatch from rustc_middle to rustc_mir_transform.
Because it's only used in `rustc_mir_transform`. (Presumably it is
currently in `rustc_middle` because lots of other MIR-related stuff is,
but that's not a hard requirement.) And because `rustc_middle` is huge
and it's always good to make it smaller.
2025-02-14 16:15:57 +11:00
bjorn3
1fcae03369 Rustfmt 2025-02-08 22:12:13 +00:00
Nicholas Nethercote
d28678e621 Clean up trivial traversal/lift impl generator macro calls.
We have four macros for generating trivial traversal (fold/visit) and
lift impls.
- `rustc_ir::TrivialTypeTraversalImpls`
- `rustc_middle::TrivialTypeTraversalImpls`
- `rustc_middle::TrivialLiftImpls`
- `rustc_middle::TrivialTypeTraversalAndLiftImpls`

The first two are very similar. The last one just combines the second
and third one.

The macros themselves are ok, but their use is a mess. This commit does
the following.
- Removes types that no longer need a lift and/or traversal impl from
  the macro calls.
- Consolidates the macro calls into the smallest number of calls
  possible, with each one mentioning as many types as possible.
- Orders the types within those macro calls alphabetically, and makes
  the module qualification more consistent.
- Eliminates `rustc_middle::mir::type_foldable`, because the macro calls
  were merged and the manual `TypeFoldable` impls are better placed in
  `structural_impls.rs`, alongside all the other ones.

This makes the code more concise. Moving forward, it also makes it more
obvious where new types should be added.
2025-02-06 13:31:43 +11:00
Nicholas Nethercote
140817380c Move find_self_call.
It's a function that does stuff with MIR and yet it weirdly has its own
module in `rustc_middle::util`. This commit moves it into
`rustc_middle::mir`, a more sensible home.
2025-01-31 16:04:13 +11:00
Zalathar
8a6e06fecb Exclude mir::coverage types from TypeFoldable/TypeVisitable
These types are unlikely to ever contain type information in the foreseeable
future, so excluding them from TypeFoldable/TypeVisitable avoids some unhelpful
derive boilerplate.
2025-01-24 16:06:18 +11:00
Rémy Rakic
a13354bea0 rename BitSet to DenseBitSet
This should make it clearer that this bitset is dense, with the
advantages and disadvantages that it entails.
2025-01-11 11:34:01 +00:00
DianQK
93aea1d0fe
mir: require is_cleanup when creating BasicBlockData 2024-12-18 20:43:54 +08:00
Nicholas Nethercote
2620eb42d7 Re-export more rustc_span::symbol things from rustc_span.
`rustc_span::symbol` defines some things that are re-exported from
`rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some
closely related things such as `Ident` and `kw`. So you can do `use
rustc_span::{Symbol, sym}` but you have to do `use
rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good
reason.

This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`,
and changes many `rustc_span::symbol::` qualifiers in `compiler/` to
`rustc_span::`. This is a 200+ net line of code reduction, mostly
because many files with two `use rustc_span` items can be reduced to
one.
2024-12-18 13:38:53 +11:00
lcnr
a8c8ab1acd remove remaining references to Reveal 2024-11-23 13:52:56 +01:00
lcnr
002efeb72a additional TypingEnv cleanups 2024-11-19 21:36:23 +01:00
lcnr
9cba14b95b use TypingEnv when no infcx is available
the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
2024-11-18 10:38:56 +01:00
Jubilee Young
236fe33345 compiler: Directly use rustc_abi in metadata and middle
Stop reexporting ReprOptions from middle::ty
2024-11-03 13:38:47 -08:00
lcnr
2cde638ac0 stop using ParamEnv::reveal while handling MIR 2024-10-31 14:55:53 +01:00
Michael Goulet
c682aa162b Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
Ben Kimock
0ea5dc506f Don't alloca for unused locals 2024-09-21 01:06:59 -04:00
bors
a5efa01895 Auto merge of #107251 - dingxiangfei2009:let-chain-rescope, r=jieyouxu
Rescope temp lifetime in if-let into IfElse with migration lint

Tracking issue #124085

This PR shortens the temporary lifetime to cover only the pattern matching and consequent branch of a `if let`.

At the expression location, means that the lifetime is shortened from previously the deepest enclosing block or statement in Edition 2021. This warrants an Edition change.

Coming with the Edition change, this patch also implements an edition lint to warn about the change and a safe rewrite suggestion to preserve the 2021 semantics in most cases.

Related to #103108.
Related crater runs: https://github.com/rust-lang/rust/pull/129466.
2024-09-13 03:47:30 +00:00
Ding Xiang Fei
f93df1f7dc
rescope temp lifetime in let-chain into IfElse
apply rules by span edition
2024-09-11 04:10:00 +08:00
Eduardo Sánchez Muñoz
0b20ffcb63 Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
Nicholas Nethercote
2aae619edb Move MirPass to rustc_mir_transform.
Because that's now the only crate that uses it.

Moving stuff out of `rustc_middle` is always welcome.

I chose to use `impl crate::MirPass`/`impl crate::MirLint` (with
explicit `crate::`) everywhere because that's the only mention of
`MirPass`/`MirLint` used in all of these files. (Prior to this change,
`MirPass` was mostly imported via `use rustc_middle::mir::*` items.)
2024-09-03 16:03:46 +10:00
Michael Goulet
4609841c07 Stop using a special inner body for the coroutine by-move body for async closures 2024-08-26 18:44:19 -04:00
kyoto7250
df568af244 fix link in mir/mod
change url path when rewrite those code
2024-08-21 00:14:04 +09:00
Ralf Jung
6d312d7bd1 MIR required_consts, mentioned_items: ensure we do not forget to fill these lists 2024-08-01 15:49:25 +02:00
Nicholas Nethercote
84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Zalathar
f095de4bf1 coverage: Rename mir::coverage::BranchInfo to CoverageInfoHi
This opens the door to collecting and storing coverage information that is
unrelated to branch coverage or MC/DC.
2024-07-05 13:53:05 +10:00
Scott McMurray
b28efb11af Save 2 pointers in TerminatorKind (96 → 80 bytes)
These things don't need to be `Vec`s; boxed slices are enough.

The frequent one here is call arguments, but MIR building knows the number of arguments from the THIR, so the collect is always getting the allocation right in the first place, and thus this shouldn't ever add the shrink-in-place overhead.
2024-06-21 18:02:05 -07:00