summary refs log tree commit diff
path: root/compiler/rustc_hir/src
AgeCommit message (Collapse)AuthorLines
2024-01-28hir: Use `InferArg` in `ArrayLen::Infer`Vadim Petrochenkov-4/+6
2024-01-28hir: Remove unnecessary `HirId` from `hir::Let`Vadim Petrochenkov-13/+6
It has 1-to-1 correspondence to its expression id. Also remove mostly useless `visit_let_expr`.
2024-01-26Auto merge of #119968 - clubby789:unused-feature, r=compiler-errorsbors-1/+0
Remove unused/unnecessary features ~~The bulk of the actual code changes here is replacing try blocks with equivalent closures. I'm not entirely sure that's a good idea since it may have perf impact, happy to revert if that's the case/the change is unwanted.~~ I also removed a lot of `recursion_limit = "256"` since everything seems to build fine without that and most don't have any comment justifying it.
2024-01-26Auto merge of #116167 - RalfJung:structural-eq, r=lcnrbors-2/+0
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust https://github.com/rust-lang/rust/pull/115893 to check for `Eq`, and rule out float matching for good. Fixes https://github.com/rust-lang/rust/issues/115881
2024-01-25Remove unused featuresclubby789-1/+0
2024-01-25Rollup merge of #119305 - compiler-errors:async-fn-traits, r=oli-obkMatthias Krüger-0/+4
Add `AsyncFn` family of traits I'm proposing to add a new family of `async`hronous `Fn`-like traits to the standard library for experimentation purposes. ## Why do we need new traits? On the user side, it is useful to be able to express `AsyncFn` trait bounds natively via the parenthesized sugar syntax, i.e. `x: impl AsyncFn(&str) -> String` when experimenting with async-closure code. This also does not preclude `AsyncFn` becoming something else like a trait alias if a more fundamental desugaring (which can take many[^1] different[^2] forms) comes around. I think we should be able to play around with `AsyncFn` well before that, though. I'm also not proposing stabilization of these trait names any time soon (we may even want to instead express them via new syntax, like `async Fn() -> ..`), but I also don't think we need to introduce an obtuse bikeshedding name, since `AsyncFn` just makes sense. ## The lending problem: why not add a more fundamental primitive of `LendingFn`/`LendingFnMut`? Firstly, for `async` closures to be as flexible as possible, they must be allowed to return futures which borrow from the async closure's captures. This can be done by introducing `LendingFn`/`LendingFnMut` traits, or (equivalently) by adding a new generic associated type to `FnMut` which allows the return type to capture lifetimes from the `&mut self` argument of the trait. This was proposed in one of [Niko's blog posts](https://smallcultfollowing.com/babysteps/blog/2023/05/09/giving-lending-and-async-closures/). Upon further experimentation, for the purposes of closure type- and borrow-checking, I've come to the conclusion that it's significantly harder to teach the compiler how to handle *general* lending closures which may borrow from their captures. This is, because unlike `Fn`/`FnMut`, the `LendingFn`/`LendingFnMut` traits don't form a simple "inheritance" hierarchy whose top trait is `FnOnce`. ```mermaid flowchart LR Fn FnMut FnOnce LendingFn LendingFnMut Fn -- isa --> FnMut FnMut -- isa --> FnOnce LendingFn -- isa --> LendingFnMut Fn -- isa --> LendingFn FnMut -- isa --> LendingFnMut ``` For example: ``` fn main() { let s = String::from("hello, world"); let f = move || &s; let x = f(); // This borrows `f` for some lifetime `'1` and returns `&'1 String`. ``` That trait hierarchy means that in general for "lending" closures, like `f` above, there's not really a meaningful return type for `<typeof(f) as FnOnce>::Output` -- it can't return `&'static str`, for example. ### Special-casing this problem: By splitting out these traits manually, and making sure that each trait has its own associated future type, we side-step the issue of having to answer the questions of a general `LendingFn`/`LendingFnMut` implementation, since the compiler knows how to generate built-in implementations for first-class constructs like async closures, including the required future types for the (by-move) `AsyncFnOnce` and (by-ref) `AsyncFnMut`/`AsyncFn` trait implementations. [^1]: For example, with trait transformers, we may eventually be able to write: `trait AsyncFn = async Fn;` [^2]: For example, via the introduction of a more fundamental "`LendingFn`" trait, plus a [special desugaring with augmented trait aliases](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Lending.20closures.20and.20Fn*.28.29.20-.3E.20impl.20Trait/near/408471480).
2024-01-24remove StructuralEq traitRalf Jung-2/+0
2024-01-22Rollup merge of #120143 - ↵Matthias Krüger-0/+3
compiler-errors:consolidate-instance-resolve-for-coroutines, r=oli-obk Consolidate logic around resolving built-in coroutine trait impls Deduplicates a lot of code. Requires defining a new lang item for `Coroutine::resume` for consistency, but it seems not harmful at worst, and potentially later useful at best. r? oli-obk
2024-01-21Auto merge of #120100 - oli-obk:astconv_lifetimes, r=BoxyUwUbors-1/+1
Don't forget that the lifetime on hir types is `'tcx` This PR just tracks the `'tcx` lifetime to wherever the original objects actually have that lifetime. This code is needed for https://github.com/rust-lang/rust/pull/107606 (now #120131) so that `ast_ty_to_ty` can invoke `lit_to_const` on an argument passed to it. Currently the argument is `&hir::Ty<'_>`, but after this PR it is `&'tcx hir::Ty<'tcx>`.
2024-01-19Consolidate logic around resolving built-in coroutine trait implsMichael Goulet-0/+3
2024-01-19Auto merge of #120006 - cjgillot:no-hir-owner, r=wesleywiserbors-9/+2
Get rid of the hir_owner query. This query was meant as a firewall between `hir_owner_nodes` which is supposed to change often, and the queries that only depend on the item signature. That firewall was inefficient, leaking the contents of the HIR body through `HirId`s. `hir_owner` incurs a significant cost, as we need to hash HIR twice in multiple modes. This PR proposes to remove it, and simplify the hashing scheme. For the future, `def_kind`, `def_span`... are much more efficient for incremental decoupling, and should be preferred.
2024-01-18Don't forget that the lifetime on hir types is `'tcx`Oli Scherer-1/+1
2024-01-17Add `PatKind::Err`Lieselotte-3/+6
2024-01-16Simplify BodyId hashing.Camille GILLOT-8/+1
2024-01-16Get rid of the hir_owner query.Camille GILLOT-1/+1
2024-01-14Auto merge of #119361 - sjwang05:issue-119352, r=WaffleLapkinbors-0/+27
Fix ICE when suggesting dereferencing binop operands Fixes #119352
2024-01-12Fix ICE when suggesting dereferencing binop operandssjwang05-0/+27
2024-01-12Delegation implementation: step 1Bryanskiy-1/+9
2024-01-10Turn some free functions into methodsOli Scherer-1/+43
2024-01-09Auto merge of #117703 - compiler-errors:recursive-async, r=lcnrbors-0/+6
Support async recursive calls (as long as they have indirection) Before #101692, we stored coroutine witness types directly inside of the coroutine. That means that a coroutine could not contain itself (as a witness field) without creating a cycle in the type representation of the coroutine, which we detected with the `OpaqueTypeExpander`, which is used to detect cycles when expanding opaque types after that are inferred to contain themselves. After `-Zdrop-tracking-mir` was stabilized, we no longer store these generator witness fields directly, but instead behind a def-id based query. That means there is no technical obstacle in the compiler preventing coroutines from containing themselves per se, other than the fact that for a coroutine to have a non-infinite layout, it must contain itself wrapped in a layer of allocation indirection (like a `Box`). This means that it should be valid for this code to work: ``` async fn async_fibonacci(i: u32) -> u32 { if i == 0 || i == 1 { i } else { Box::pin(async_fibonacci(i - 1)).await + Box::pin(async_fibonacci(i - 2)).await } } ``` Whereas previously, you'd need to coerce the future to `Pin<Box<dyn Future<Output = ...>>` before `await`ing it, to prevent the async's desugared coroutine from containing itself across as await point. This PR does two things: 1. Only report an error if an opaque expansion cycle is detected *not* through coroutine witness fields. * Instead, if we find an opaque cycle through coroutine witness fields, we compute the layout of the coroutine. If that results in a cycle error, we report it as a recursive async fn. 4. Reworks the way we report layout errors having to do with coroutines, to make up for the diagnostic regressions introduced by (1.). We actually do even better now, pointing out the call sites of the recursion!
2024-01-09Rollup merge of #119704 - chenyukang:yukang-fix-let_underscore, r=NilstriebMatthias Krüger-1/+1
Fix two variable binding issues in lint let_underscore Fixes #119696 Fixes #119697
2024-01-08Don't check for recursion in generator witness fieldsMichael Goulet-0/+6
2024-01-08Fix 2 variable binding issues in let_underscoreyukang-1/+1
2024-01-05Remove `hir::Guard`Matthew Jasper-28/+3
Use Expr instead. Use `ExprKind::Let` to represent if let guards.
2024-01-03Rollup merge of #119239 - compiler-errors:yield-coercion, r=davidtwcoLeón Orell Valerian Liehr-6/+0
Remove unnecessary arm in `check_expr_yield` We always set up the `resume_yield_tys` for async blocks and fns, so this arm was unreachable.
2023-12-29Auto merge of #119259 - cjgillot:single-crate-id, r=Mark-Simulacrumbors-30/+36
Only store StableCrateId once in DefPathTable. https://github.com/rust-lang/rust/pull/119238 made me think of this. cc `@Mark-Simulacrum`
2023-12-27Introduce `const Trait` (always-const trait bounds)León Orell Valerian Liehr-0/+6
2023-12-25Only regular coroutines have movabilityMichael Goulet-6/+14
2023-12-25select AsyncFn traits during overloaded call opMichael Goulet-0/+4
2023-12-25Make closures carry their own ClosureKind, rather than deducing what it is ↵Michael Goulet-8/+17
from movability
2023-12-24Do not store stable crate id in on-disk hash map.Camille GILLOT-11/+13
2023-12-24Do not store stable_crate_id again in Definitions.Camille GILLOT-5/+2
2023-12-24Only store StableCrateId once in DefPathTable.Camille GILLOT-15/+22
2023-12-23Remove unnecessary arm in check_expr_yieldMichael Goulet-6/+0
2023-12-22Rollup merge of #119222 - eholk:into-async-iterator, r=compiler-errors,dtolnayMichael Goulet-0/+1
Add `IntoAsyncIterator` This introduces the `IntoAsyncIterator` trait and uses it in the desugaring of the unstable `for await` loop syntax. This is mostly added for symmetry with `Iterator` and `IntoIterator`. r? `@compiler-errors` cc `@rust-lang/libs-api,` `@rust-lang/wg-async`
2023-12-22Split coroutine desugaring kind from sourceMichael Goulet-31/+47
2023-12-22Use `IntoAsyncIterator` in `for await` loop desugaringEric Holk-0/+1
2023-12-22Auto merge of #118847 - eholk:for-await, r=compiler-errorsbors-0/+2
Add support for `for await` loops This adds support for `for await` loops. This includes parsing, desugaring in AST->HIR lowering, and adding some support functions to the library. Given a loop like: ```rust for await i in iter { ... } ``` this is desugared to something like: ```rust let mut iter = iter.into_async_iter(); while let Some(i) = loop { match core::pin::Pin::new(&mut iter).poll_next(cx) { Poll::Ready(i) => break i, Poll::Pending => yield, } } { ... } ``` This PR also adds a basic `IntoAsyncIterator` trait. This is partly for symmetry with the way `Iterator` and `IntoIterator` work. The other reason is that for async iterators it's helpful to have a place apart from the data structure being iterated over to store state. `IntoAsyncIterator` gives us a good place to do this. I've gated this feature behind `async_for_loop` and opened #118898 as the feature tracking issue. r? `@compiler-errors`
2023-12-22Auto merge of #119163 - fmease:refactor-ast-trait-bound-modifiers, ↵bors-2/+1
r=compiler-errors Refactor AST trait bound modifiers Instead of having two types to represent trait bound modifiers in the parser / the AST (`parser::ty::BoundModifiers` & `ast::TraitBoundModifier`), only to map one to the other later, just use `parser::ty::BoundModifiers` (moved & renamed to `ast::TraitBoundModifiers`). The struct type is more extensible and easier to deal with (see [here](https://github.com/rust-lang/rust/pull/119099/files#r1430749981) and [here](https://github.com/rust-lang/rust/pull/119099/files#r1430752116) for context) since it more closely models what it represents: A compound of two kinds of modifiers, constness and polarity. Modeling this as an enum (the now removed `ast::TraitBoundModifier`) meant one had to add a new variant per *combination* of modifier kind, which simply isn't scalable and which lead to a lot of explicit non-DRY matches. NB: `hir::TraitBoundModifier` being an enum is fine since HIR doesn't need to worry representing invalid modifier kind combinations as those get rejected during AST validation thereby immensely cutting down the number of possibilities.
2023-12-20Refactor AST trait bound modifiersLeón Orell Valerian Liehr-2/+1
2023-12-20Give `VariantData::Struct` named fields, to clairfy `recovered`.Alona Enraght-Moony-4/+8
2023-12-19Desugar for await loopsEric Holk-0/+2
2023-12-18Replace some instances of FxHashMap/FxHashSet with stable alternatives ↵Michael Woerister-16/+15
(mostly in rustc_hir and rustc_ast_lowering) Part of https://github.com/rust-lang/compiler-team/issues/533
2023-12-15Rollup merge of #119004 - matthiaskrgr:conv, r=compiler-errorsJubilee-6/+4
NFC don't convert types to identical types
2023-12-15NFC don't convert types to identical typesMatthias Krüger-6/+4
2023-12-15Rollup merge of #118396 - compiler-errors:ast-lang-items, r=cjgillotJubilee-7/+36
Collect lang items from AST, get rid of `GenericBound::LangItemTrait` r? `@cjgillot` cc #115178 Looking forward, the work to remove `QPath::LangItem` will also be significantly more difficult, but I plan on doing it as well. Specifically, we have to change: 1. A lot of `rustc_ast_lowering` for things like expr `..` 2. A lot of astconv, since we actually instantiate lang and non-lang paths quite differently. 3. A ton of diagnostics and clippy lints that are special-cased via `QPath::LangItem` Meanwhile, it was pretty easy to remove `GenericBound::LangItemTrait`, so I just did that here.
2023-12-15Fix enforcement of generics for associated itemsMichael Goulet-0/+36
2023-12-15banish hir::GenericBound::LangItemTraitMichael Goulet-7/+0
2023-12-15Annotate some bugsMichael Goulet-1/+1
2023-12-08Auto merge of #118420 - compiler-errors:async-gen, r=eholkbors-17/+18
Introduce support for `async gen` blocks I'm delighted to demonstrate that `async gen` block are not very difficult to support. They're simply coroutines that yield `Poll<Option<T>>` and return `()`. **This PR is WIP and in draft mode for now** -- I'm mostly putting it up to show folks that it's possible. This PR needs a lang-team experiment associated with it or possible an RFC, since I don't think it falls under the jurisdiction of the `gen` RFC that was recently authored by oli (https://github.com/rust-lang/rfcs/pull/3513, https://github.com/rust-lang/rust/issues/117078). ### Technical note on the pre-generator-transform yield type: The reason that the underlying coroutines yield `Poll<Option<T>>` and not `Poll<T>` (which would make more sense, IMO, for the pre-transformed coroutine), is because the `TransformVisitor` that is used to turn coroutines into built-in state machine functions would have to destructure and reconstruct the latter into the former, which requires at least inserting a new basic block (for a `switchInt` terminator, to match on the `Poll` discriminant). This does mean that the desugaring (at the `rustc_ast_lowering` level) of `async gen` blocks is a bit more involved. However, since we already need to intercept both `.await` and `yield` operators, I don't consider it much of a technical burden. r? `@ghost`