Commit graph

2335 commits

Author SHA1 Message Date
Matthias Krüger
de9323973a remove a couple clones 2025-04-20 18:58:46 +02:00
Folkert de Vries
41ddf86722
Make #[naked] an unsafe attribute 2025-04-19 00:03:35 +02:00
Matthias Krüger
540fb228af
Rollup merge of #139615 - nnethercote:rm-name_or_empty, r=jdonszelmann
Remove `name_or_empty`

Another step towards #137978.

r? ``@jdonszelmann``
2025-04-18 05:16:29 +02:00
Matthias Krüger
c8a9095f0f
Rollup merge of #138528 - dianne:implicit-deref-patterns, r=Nadrieril
deref patterns: implement implicit deref patterns

This implements implicit deref patterns (per https://hackmd.io/4qDDMcvyQ-GDB089IPcHGg#Implicit-deref-patterns) and adds tests and an unstable book chapter.

Best reviewed commit-by-commit. Overall there's a lot of additions, but a lot of that is tests, documentation, and simple(?) refactoring.

Tracking issue: #87121

r? ``@Nadrieril``
2025-04-18 05:16:28 +02:00
Matthias Krüger
67e2358fbb
Rollup merge of #139902 - lcnr:no-opaque-cast-projection, r=oli-obk
do not emit `OpaqueCast` projections with `-Znext-solver`

We normalize opaque types in their defining scope if the new solver is enabled. This means projections do not contain any 'revealable' opaque types we need to worry about. We either have a type which has been normalized by writeback or we need to normalize it anyways.

r? ```@compiler-errors``` ```@oli-obk```
2025-04-17 17:40:28 +02:00
lcnr
289a23e0e2 do not emit OpaqueCast projections with -Znext-solver 2025-04-17 12:15:04 +02:00
Jake Goulding
0117884917 Move eager translation to a method on Diag
This will allow us to eagerly translate messages on a top-level
diagnostic, such as a `LintDiagnostic`. As a bonus, we can remove the
awkward closure passed into Subdiagnostic and make better use of
`Into`.
2025-04-16 21:38:59 -04:00
Nicholas Nethercote
2fef0a30ae Replace infallible name_or_empty methods with fallible name methods.
I'm removing empty identifiers everywhere, because in practice they
always mean "no identifier" rather than "empty identifier". (An empty
identifier is impossible.) It's better to use `Option` to mean "no
identifier" because you then can't forget about the "no identifier"
possibility.

Some specifics:
- When testing an attribute for a single name, the commit uses the
  `has_name` method.
- When testing an attribute for multiple names, the commit uses the new
  `has_any_name` method.
- When using `match` on an attribute, the match arms now have `Some` on
  them.

In the tests, we now avoid printing empty identifiers by not printing
the identifier in the `error:` line at all, instead letting the carets
point out the problem.
2025-04-17 09:50:52 +10:00
dianne
cb6c499bc9 lower implicit deref patterns to THIR
Since this uses `pat_adjustments`, I've also tweaked the documentation
to mention implicit deref patterns and made sure the pattern migration
diagnostic logic accounts for it. I'll adjust `ExprUseVisitor` in a
later commit and add some tests there for closure capture inference.
2025-04-16 14:42:55 -07:00
bors
f433fa46b0 Auto merge of #139845 - Zalathar:rollup-u5u5y1v, r=Zalathar
Rollup of 17 pull requests

Successful merges:

 - #138374 (Enable contracts for const functions)
 - #138380 (ci: add runners for vanilla LLVM 20)
 - #138393 (Allow const patterns of matches to contain pattern types)
 - #139517 (std: sys: process: uefi: Use NULL stdin by default)
 - #139554 (std: add Output::exit_ok)
 - #139660 (compiletest: Add an experimental new executor to replace libtest)
 - #139669 (Overhaul `AssocItem`)
 - #139671 (Proc macro span API redesign: Replace proc_macro::SourceFile by Span::{file, local_file})
 - #139750 (std/thread: Use default stack size from menuconfig for NuttX)
 - #139772 (Remove `hir::Map`)
 - #139785 (Let CStrings be either 1 or 2 byte aligned.)
 - #139789 (do not unnecessarily leak auto traits in item bounds)
 - #139791 (drop global where-bounds before merging candidates)
 - #139798 (normalize: prefer `ParamEnv` over `AliasBound` candidates)
 - #139822 (Fix: Map EOPNOTSUPP to ErrorKind::Unsupported on Unix)
 - #139833 (Fix some HIR pretty-printing problems)
 - #139836 (Basic tests of MPMC receiver cloning)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-04-15 08:02:23 +00:00
Stuart Cook
13cd5256ac
Rollup merge of #139669 - nnethercote:overhaul-AssocItem, r=oli-obk
Overhaul `AssocItem`

`AssocItem` has multiple fields that only make sense some of the time. E.g. the `name` can be empty if it's an RPITIT associated type. It's clearer and less error prone if these fields are moved to the relevant `kind` variants.

r? ``@fee1-dead``
2025-04-15 15:47:27 +10:00
Stuart Cook
aa9a80cc34
Rollup merge of #138393 - oli-obk:pattern-type-in-pattern, r=BoxyUwU
Allow const patterns of matches to contain pattern types

Trying to pattern match on a type containing a pattern type will currently fail with an ICE

```rust
error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1..
  --> src/main.rs:22:5
   |
22 |     TWO => {}
   |     ^^^
```

because the compiler tries to generate a MIR `BinOp(Eq)` operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could `==` pattern types and that it could not because `PartialEq` is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing.

r? ```@BoxyUwU```

cc #123646 ```@scottmcm``` ```@joshtriplett```
2025-04-15 15:47:25 +10:00
Yotam Ofek
4b63362f3d Use newtype_index!-generated types more idiomatically 2025-04-14 16:17:06 +00:00
Oli Scherer
7ad16974b9 Allow const patterns of matches to contain pattern types 2025-04-14 14:27:35 +00:00
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
bors
f836ae4e66 Auto merge of #124141 - nnethercote:rm-Nonterminal-and-TokenKind-Interpolated, r=petrochenkov
Remove `Nonterminal` and `TokenKind::Interpolated`

A third attempt at this; the first attempt was #96724 and the second was #114647.

r? `@ghost`
2025-04-14 03:56:55 +00:00
dianne
f35eae780b store the kind of pattern adjustments in pat_adjustments
This allows us to better distinguish builtin and overloaded implicit
dereferences.
2025-04-12 17:42:09 -07:00
Santiago Pastorino
b078564fe6
Make use generated TerminatorKind::Call have call_source Use 2025-04-07 16:53:11 -03:00
Stuart Cook
9955b7634d
Rollup merge of #139108 - Nadrieril:simplify-expandedconstant, r=oli-obk
Simplify `thir::PatKind::ExpandedConstant`

I made it a bit less ad-hoc. In particular, I removed `is_inline: bool` that was just caching the output of `tcx.def_kind(def_id)`. This makes inline consts a tiny bit less special in patterns.

r? `@oli-obk`

cc `@Zalathar`
2025-04-07 22:29:18 +10:00
Stuart Cook
82df6229b6
Rollup merge of #139035 - nnethercote:PatKind-Missing, r=oli-obk
Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with #137978. Details in the individual commits.

r? ``@oli-obk``
2025-04-07 22:29:17 +10:00
Nadrieril
d912c03525 Reuse parent_args 2025-04-06 17:30:05 +02:00
Nadrieril
961c7463d8 Add the inline const type annotation in pattern lowering 2025-04-06 17:30:05 +02:00
Nadrieril
50e10b37c0 Tweak lower_pat_expr 2025-04-06 17:30:04 +02:00
Nadrieril
090d76497f Remove the is_inline field from PatKind::ExpandedConstant 2025-04-06 17:27:27 +02:00
Nadrieril
de57c0515d Let const_to_pat handle the ExpandedConstant wrapping 2025-04-06 17:27:27 +02:00
Matthias Krüger
48a3919884
Rollup merge of #138610 - oli-obk:no-sort-hir-ids, r=compiler-errors
impl !PartialOrd for HirId

revive of https://github.com/rust-lang/rust/pull/92233

Another checkbox of https://github.com/rust-lang/rust/issues/90317, another small step in making incremental less likely to die in horrible ways
2025-04-03 21:18:30 +02:00
Matthias Krüger
e5c7451a10
Rollup merge of #138017 - nnethercote:tighten-assignment-op, r=spastorino
Tighten up assignment operator representations.

This is step 3 of [MCP 831](https://github.com/rust-lang/compiler-team/issues/831).

r? `@spastorino`
2025-04-03 21:18:28 +02:00
Oli Scherer
0795b3de9b Upvars HirIds always have the same owner, thus just use an ItemLocalId 2025-04-03 09:22:21 +00:00
Oli Scherer
c51816ee59 Make LevelAndSource a struct 2025-04-03 09:17:55 +00:00
bors
3658060890 Auto merge of #139234 - compiler-errors:query-tweak, r=oli-obk
Misc query tweaks

Remove some redundant work around `cache_on_disk` and `ensure_ok`, since `Result<(), ErrorGuaranteed>` queries don't need to cache or recompute their "value" if they are only used for their result.
2025-04-03 00:13:54 +00:00
Nicholas Nethercote
ddcb370bc6 Tighten up assignment operator representations.
In the AST, currently we use `BinOpKind` within `ExprKind::AssignOp` and
`AssocOp::AssignOp`, even though this allows some nonsensical
combinations. E.g. there is no `&&=` operator. Likewise for HIR and
THIR.

This commit introduces `AssignOpKind` which only includes the ten
assignable operators, and uses it in `ExprKind::AssignOp` and
`AssocOp::AssignOp`. (And does similar things for `hir::ExprKind` and
`thir::ExprKind`.) This avoids the possibility of nonsensical
combinations, as seen by the removal of the `bug!` case in
`lang_item_for_binop`.

The commit is mostly plumbing, including:
- Adds an `impl From<AssignOpKind> for BinOpKind` (AST) and `impl
  From<AssignOp> for BinOp` (MIR/THIR).
- `BinOpCategory` can now be created from both `BinOpKind` and
  `AssignOpKind`.
- Replaces the `IsAssign` type with `Op`, which has more information and
  a few methods.
- `suggest_swapping_lhs_and_rhs`: moves the condition to the call site,
  it's easier that way.
- `check_expr_inner`: had to factor out some code into a separate
  method.

I'm on the fence about whether avoiding the nonsensical combinations is
worth the extra code.
2025-04-03 10:23:03 +11:00
bors
d5b4c2e4f1 Auto merge of #139269 - matthiaskrgr:rollup-pk78gig, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #138992 (literal pattern lowering: use the pattern's type instead of the literal's in `const_to_pat`)
 - #139211 (interpret: add a version of run_for_validation for &self)
 - #139235 (`AstValidator` tweaks)
 - #139237 (Add a dep kind for use of the anon node with zero dependencies)
 - #139260 (Add dianqk to codegen reviewers)
 - #139264 (Fix two incorrect turbofish suggestions)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-04-02 18:39:21 +00:00
Matthias Krüger
3fb1230adc
Rollup merge of #138992 - dianne:simplify-byte-string-to-pat, r=oli-obk
literal pattern lowering: use the pattern's type instead of the literal's in `const_to_pat`

This has two purposes:
- First, it enables removing the `treat_byte_string_as_slice` fields from `TypeckResults` and `ConstToPat`. A byte string pattern's type will be `&[u8]` when matching on a slice reference, so `const_to_pat` will lower it to a slice ref pattern. I believe this is tested by `tests/ui/match/pattern-deref-miscompile.rs`.
- Second, it will simplify the implementation of byte string literals in deref patterns. If byte string patterns can be given the type `[u8; N]` or `[u8]` during HIR typeck, then nothing needs to be changed in `const_to_pat` in order to lower the patterns `deref!(b"..."): Vec<u8>` and `deref!(b"..."): Box<[u8; 3]>`.

Implementation-wise, this uses `lit_to_const` to make a const with the pattern's type and the literal's valtree; that feels to me like the best way to make sure that the valtree representations of the pattern type and literal are the same. Though it may necessitate later changes to `lit_to_const` to accommodate giving byte string literal patterns non-reference types—would that be reasonable?

This unfortunately doesn't work for the `string_deref_patterns` feature (since that gives string literal patterns the `String` type), so I added a workaround for that. However, once `deref_patterns` supports string literals, it may be able to replace `string_deref_patterns`; the special case for `String` can removed at that point.

r? ``@oli-obk``
2025-04-02 19:44:12 +02:00
Nicholas Nethercote
1830245a22 Remove recursion_limit increases.
These are no longer needed now that `Nonterminal` is gone.
2025-04-02 16:25:27 +11:00
Michael Goulet
444a7eb5aa Use return_result_from_ensure_ok a bit more 2025-04-02 04:01:15 +00:00
Nicholas Nethercote
6713f34ee4 Move methods from Map to TyCtxt, part 5.
This eliminates all methods on `Map`. Actually removing `Map` will occur
in a follow-up PR.
2025-04-02 10:00:46 +11:00
Yotam Ofek
9ef35ddc0c use slice::contains where applicable 2025-03-28 12:21:21 +00:00
Nicholas Nethercote
9f089e080c Add {ast,hir,thir}::PatKind::Missing variants.
"Missing" patterns are possible in bare fn types (`fn f(u32)`) and
similar places. Currently these are represented in the AST with
`ast::PatKind::Ident` with no `by_ref`, no `mut`, an empty ident, and no
sub-pattern. This flows through to `{hir,thir}::PatKind::Binding` for
HIR and THIR.

This is a bit nasty. It's very non-obvious, and easy to forget to check
for the exceptional empty identifier case.

This commit adds a new variant, `PatKind::Missing`, to do it properly.

The process I followed:
- Add a `Missing` variant to `{ast,hir,thir}::PatKind`.
- Chang `parse_param_general` to produce `ast::PatKind::Missing`
  instead of `ast::PatKind::Missing`.
- Look through `kw::Empty` occurrences to find functions where an
  existing empty ident check needs replacing with a `PatKind::Missing`
  check: `print_param`, `check_trait_item`, `is_named_param`.
- Add a `PatKind::Missing => unreachable!(),` arm to every exhaustive
  match identified by the compiler.
- Find which arms are actually reachable by running the test suite,
  changing them to something appropriate, usually by looking at what
  would happen to a `PatKind::Ident`/`PatKind::Binding` with no ref, no
  `mut`, an empty ident, and no subpattern.

Quite a few of the `unreachable!()` arms were never reached. This makes
sense because `PatKind::Missing` can't happen in every pattern, only
in places like bare fn tys and trait fn decls.

I also tried an alternative approach: modifying `ast::Param::pat` to
hold an `Option<P<Pat>>` instead of a `P<Pat>`, but that quickly turned
into a very large and painful change. Adding `PatKind::Missing` is much
easier.
2025-03-28 09:18:57 +11:00
dianne
3779c6b797 add a temporary workaround for string_deref_patterns 2025-03-26 10:10:28 -07:00
dianne
0a05677d22 lower_pat_expr: use the pattern's type instead of the literal's
This allows us to remove the field `treat_byte_string_as_slice` from
`TypeckResults`, since the pattern's type contains everything necessary
to get the correct lowering for byte string literal patterns.

This leaves the implementation of `string_deref_patterns` broken, to be
fixed in the next commit.
2025-03-26 10:10:28 -07:00
Stuart Cook
33c90235a1
Rollup merge of #138959 - meithecatte:matchpair-place-option, r=Zalathar
Revert "Make MatchPairTree::place non-optional"

Reverts a part of #137875. Fixes #138958.

cc `@Zalathar`
2025-03-26 19:40:31 +11:00
Stuart Cook
ff325e0a00
Rollup merge of #138818 - khuey:138198, r=jieyouxu
Don't produce debug information for compiler-introduced-vars when desugaring assignments.

An assignment such as

(a, b) = (b, c);

desugars to the HIR

{ let (lhs, lhs) = (b, c); a = lhs; b = lhs; };

The repeated `lhs` leads to multiple Locals assigned to the same DILocalVariable. Rather than attempting to fix that, get rid of the debug info for these bindings that don't even exist in the program to begin with.

Fixes #138198

r? `@jieyouxu`
2025-03-26 19:40:28 +11:00
Maja Kądziołka
c8a5b3677b
MatchPairTree: update invariant comment 2025-03-26 02:18:13 +01:00
Maja Kądziołka
0c162b19ec
Revert "Make MatchPairTree::place non-optional"
This reverts commit e3e74bc89a.

The comment that was used to justify the change was outdated.
2025-03-26 02:09:13 +01:00
Matthias Krüger
dfd83be4da
Rollup merge of #138821 - dianne:cleanup-non-scalar-compare, r=oli-obk
match lowering cleanup: remove unused unsizing logic from `non_scalar_compare`

Since array and slice constants are now translated to array and slice patterns, `non_scalar_compare` is only used for string comparisons. This specializes it to strings, renames it, and removes the unused array-unsizing logic.

This also updates the doc comments for  `thir::PatKind::Constant` and `TestKind::Eq`, which referred to them being used for slice references.

r? ````@oli-obk````
2025-03-24 20:40:08 +01:00
Matthias Krüger
92acd148f8
Rollup merge of #138410 - bjorn3:misc_cleanups, r=compiler-errors
Couple mir building cleanups
2025-03-22 11:59:15 +01:00
dianne
83145a6744 match lowering cleanup: non_scalar_compare is only for &str
Since array and slice constants are now lowered to array and slice
patterns, `non_scalar_compare` was only called for string comparisons.
This specializes it to strings, renames it, and removes the unused
array-unsizing logic.

This also updates some outdated doc comments.
2025-03-21 20:14:40 -07:00
Kyle Huey
8cab8e07bc Don't produce debug information for compiler-introduced-vars when desugaring assignments.
An assignment such as

(a, b) = (b, c);

desugars to the HIR

{ let (lhs, lhs) = (b, c); a = lhs; b = lhs; };

The repeated `lhs` leads to multiple Locals assigned to the same DILocalVariable. Rather than
attempting to fix that, get rid of the debug info for these bindings that don't even exist
in the program to begin with.

Fixes #138198
2025-03-21 17:34:45 -07:00
bors
4e2b096ed6 Auto merge of #137930 - nnethercote:use-Wunused-crate-dependencies, r=jieyouxu,Nadrieril
Use `Wunused-crate-dependencies` for the compiler

An implementation of https://github.com/rust-lang/compiler-team/issues/844.

r? `@jieyouxu`
2025-03-20 04:20:13 +00:00
Nicholas Nethercote
8121958fda Use -Wunused_crate_dependencies for compiler crates.
It's very useful. There are some false positives involving integration
tests in `rustc_pattern_analysis` and `rustc_serialize`. There is also a
false positive involving `rustc_driver_impl`'s
`rustc_randomized_layouts` feature. And I removed a `rustc_span` mention
in a doc comment in `rustc_log` because it wasn't integral to the
comment but caused a dev-dependency.
2025-03-20 08:59:43 +11:00