about summary refs log tree commit diff
path: root/compiler/rustc_infer/src
AgeCommit message (Collapse)AuthorLines
2023-02-08Rollup merge of #107780 - compiler-errors:instantiate-binder, r=lcnrMatthias Krüger-8/+15
Rename `replace_bound_vars_with_*` to `instantiate_binder_with_*` Mentioning "binder" rather than "bound vars", imo, makes it clearer that we're doing something to the binder as a whole. Also, "instantiate" is the verb that I'm always reaching for when I'm looking for these functions, and the name that we use in the new solver anyways. r? types
2023-02-07Replacing bound vars is actually instantiating a binderMichael Goulet-8/+15
2023-02-07Auto merge of #107768 - matthiaskrgr:rollup-9u4cal4, r=matthiaskrgrbors-25/+16
Rollup of 8 pull requests Successful merges: - #107719 (Remove `arena_cache` modifier from `upstream_monomorphizations_for`) - #107740 (Avoid locking the global context across the `after_expansion` callback) - #107746 (Split fn_ctxt/adjust_fulfillment_errors from fn_ctxt/checks) - #107749 (allow quick-edit convenience) - #107750 (make more readable) - #107755 (remove binder from query constraints) - #107756 (miri: fix ICE when running out of address space) - #107764 (llvm-16: Use Triple.h from new header location.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2023-02-07Rollup merge of #107709 - tialaramex:master, r=compiler-errorsMatthias Krüger-1/+2
Fix problem noticed in PR106859 with char -> u8 suggestion HN reader `@ayosec` noticed that my #106859 a few weeks back, malfunctions if you have a Unicode escape, the code suggested b'\u{0}' if you tried to use '\u{0}' where a byte should be, when of course b'\u{0}' is not a byte literal, regardless of the codepoint you can't write Unicode escapes in a byte literal at all. My proposed fix here just checks that the "character" you wrote is fewer than 5 bytes, thus allowing \x7F and similar escapes but conveniently forbidding even the smallest Unicode escape \u{0} before offering the suggestion as before. I have provided an updated test which includes examples which do and don't work because of this additional rule.
2023-02-07Rollup merge of #107555 - edward-shen:edward-shen/dup-trait-suggestion, ↵Matthias Krüger-29/+66
r=compiler-errors Modify existing bounds if they exist Fixes #107335. This implementation is kinda gross but I don't really see a better way to do it. This primarily does two things: Modifies `suggest_constraining_type_param` to accept a new parameter that indicates a span to be replaced instead of added, if presented, and limit the additive suggestions to either suggest a new bound on an existing bound (see newly added unit test) or add the generics argument if a generics argument wasn't found. The former change is required to retain the capability to add an entirely new bounds if it was entirely omitted. r? ``@compiler-errors``
2023-02-07remove binder from query constraintslcnr-25/+16
2023-02-06Fix problem noticed in PR106859 with char -> u8 suggestionNick Lamb-1/+2
2023-02-06Rollup merge of #106477 - ↵Matthias Krüger-24/+26
Nathan-Fenner:nathanf/refined-error-span-trait-impl, r=compiler-errors Refine error spans for "The trait bound `T: Trait` is not satisfied" when passing literal structs/tuples This PR adds a new heuristic which refines the error span reported for "`T: Trait` is not satisfied" errors, by "drilling down" into individual fields of structs/enums/tuples to point to the "problematic" value. Here's a self-contained example of the difference in error span: ```rs struct Burrito<Filling> { filling: Filling, } impl <Filling: Delicious> Delicious for Burrito<Filling> {} fn eat_delicious_food<Food: Delicious>(food: Food) {} fn will_type_error() { eat_delicious_food(Burrito { filling: Kale }); // ^~~~~~~~~~~~~~~~~~~~~~~~~ (before) The trait bound `Kale: Delicious` is not satisfied // ^~~~ (after) The trait bound `Kale: Delicious` is not satisfied } ``` (kale is fine, this is just a silly food-based example) Before this PR, the error span is identified as the entire argument to the generic function `eat_delicious_food`. However, since only `Kale` is the "problematic" part, we can point at it specifically. In particular, the primary error message itself mentions the missing `Kale: Delicious` trait bound, so it's much clearer if this part is called out explicitly. --- The _existing_ heuristic tries to label the right function argument in `point_at_arg_if_possible`. It goes something like this: - Look at the broken base trait `Food: Delicious` and find which generics it mentions (in this case, only `Food`) - Look at the parameter type definitions and find which of them mention `Filling` (in this case, only `food`) - If there is exactly one relevant parameter, label the corresponding argument with the error span, instead of the entire call This PR extends this heuristic by further refining the resulting expression span in the new `point_at_specific_expr_if_possible` function. For each `impl` in the (broken) chain, we apply the following strategy: The strategy to determine this span involves connecting information about our generic `impl` with information about our (struct) type and the (struct) literal expression: - Find the `impl` (`impl <Filling: Delicious> Delicious for Burrito<Filling>`) that links our obligation (`Kale: Delicious`) with the parent obligation (`Burrito<Kale>: Delicious`) - Find the "original" predicate constraint in the impl (`Filling: Delicious`) which produced our obligation. - Find all of the generics that are mentioned in the predicate (`Filling`). - Examine the `Self` type in the `impl`, and see which of its type argument(s) mention any of those generics. - Examing the definition for the `Self` type, and identify (for each of its variants) if there's a unique field which uses those generic arguments. - If there is a unique field mentioning the "blameable" arguments, use that field for the error span. Before we do any of this logic, we recursively call `point_at_specific_expr_if_possible` on the parent obligation. Hence we refine the `expr` "outwards-in" and bail at the first kind of expression/impl we don't recognize. This function returns a `Result<&Expr, &Expr>` - either way, it returns the `Expr` whose span should be reported as an error. If it is `Ok`, then it means it refined successfull. If it is `Err`, then it may be only a partial success - but it cannot be refined even further. --- I added a new test file which exercises this new behavior. A few existing tests were affected, since their error spans are now different. In one case, this leads to a different code suggestion for the autofix - although the new suggestion isn't _wrong_, it is different from what used to be. This change doesn't create any new errors or remove any existing ones, it just adjusts the spans where they're presented. --- Some considerations: right now, this check occurs in addition to some similar logic in `adjust_fulfillment_error_for_expr_obligation` function, which tidies up various kinds of error spans (not just trait-fulfillment error). It's possible that this new code would be better integrated into that function (or another one) - but I haven't looked into this yet. Although this code only occurs when there's a type error, it's definitely not as efficient as possible. In particular, there are definitely some cases where it degrades to quadratic performance (e.g. for a trait `impl` with 100+ generic parameters or 100 levels deep nesting of generic types). I'm not sure if these are realistic enough to worry about optimizing yet. There's also still a lot of repetition in some of the logic, where the behavior for different types (namely, `struct` vs `enum` variant) is _similar_ but not the same. --- I think the biggest win here is better targeting for tuples; in particular, if you're using tuples + traits to express variadic-like functions, the compiler can't tell you which part of a tuple has the wrong type, since the span will cover the entire argument. This change allows the individual field in the tuple to be highlighted, as in this example: ``` // NEW LL | want(Wrapper { value: (3, q) }); | ---- ^ the trait `T3` is not implemented for `Q` // OLD LL | want(Wrapper { value: (3, q) }); | ---- ^~~~~~~~~~~~~~~~~~~~~~~~~ the trait `T3` is not implemented for `Q` ``` Especially with large tuples, the existing error spans are not very effective at quickly narrowing down the source of the problem.
2023-02-06Modify existing bounds if they existEdward Shen-29/+66
2023-02-06Split and inline `TypeFreshener::fold_ty`.Nicholas Nethercote-68/+61
2023-02-06Split and inline `ShallowResolver::fold_ty`.Nicholas Nethercote-24/+33
2023-02-06Inline `OpportunisticVarResolver::fold_ty`.Nicholas Nethercote-0/+1
2023-02-06Improve early bailout test in `resolve_vars_if_possible`.Nicholas Nethercote-2/+2
`!t.has_non_region_infer()` is the test used in `OpportunisticVarResolver`, and catches a few cases that `!t.needs_infer()` misses.
2023-02-06Put a `ShallowResolver` within `OpportunisticVarResolver`.Nicholas Nethercote-5/+7
So one doesn't have to be constructed every time.
2023-02-05emit `ConstEquate` in `TypeRelating<D>`Boxy-6/+7
2023-02-02Rollup merge of #107201 - compiler-errors:confusing-async-fn-note, r=estebankMatthias Krüger-46/+16
Remove confusing 'while checking' note from opaque future type mismatches Maybe I'm just misinterpreting the wording of the note. The only value I can see in this note is that it points out where the async's opaque future is coming from, but the way it's doing it is misleading IMO. For example: ```rust note: while checking the return type of the `async fn` --> $DIR/dont-suggest-missing-await.rs:7:24 | LL | async fn make_u32() -> u32 { | ^^^ checked the `Output` of this `async fn`, found opaque type ``` We point at the type `u32` in the HIR, but then say "found opaque type". We also say "while checking"... but we're typechecking a totally different function when we get this type mismatch! r? ``@estebank`` but feel free to reassign and/or take your time reviewing this. I'd be inclined to also discuss reworking the presentation of this type mismatch to restore some of these labels in a way that makes it more clear what it's trying to point out.
2023-01-31Rollup merge of #107486 - compiler-errors:bound-ty-keep-name, r=oli-obkGuillaume Gomez-4/+4
Track bound types like bound regions When we instantiate bound types into placeholder types, we throw away the names for some reason. These names are particularly useful for error reporting once we have `for<T>` binders. r? types
2023-01-30Track bound types like bound regionsMichael Goulet-4/+4
2023-01-30Make structured suggestion for fn casting verboseEsteban Küber-1/+1
2023-01-30Mention fn coercion rules (needs to be expanded)Esteban Küber-0/+19
2023-01-30review commentsEsteban Küber-1/+1
2023-01-30Modify primary span label for E0308Esteban Küber-9/+25
The previous output was unintuitive to users.
2023-01-28Auto merge of #107303 - compiler-errors:intern-canonical-var-values, r=lcnrbors-18/+15
Intern `CanonicalVarValues` So that they are copy :sparkles:
2023-01-28Rollup merge of #107385 - BoxyUwU:ConstInferUnifier_is_folder, r=compiler-errorsMatthias Krüger-88/+26
Use `FallibleTypeFolder` for `ConstInferUnifier` not `TypeRelation` I am not sure why this was using a `TypeRelation`, maybe it predates the ability to have fallible type folders
2023-01-28Rollup merge of #107339 - aliemjay:covariant, r=lcnrMatthias Krüger-7/+10
internally change regions to be covariant Surprisingly, we consider the reference type `&'a T` to be contravaraint in its lifetime parameter. This is confusing and conflicts with the documentation we have in the reference, rustnomicon, and rustc-dev-guide. This also arguably not the correct use of terminology since we can use `&'static u8` in a place where `&' a u8` is expected, this implies that `&'static u8 <: &' a u8` and consequently `'static <: ' a`, hence covariance. Because of this, when relating two types, we used to switch the argument positions in a confusing way: `Subtype(&'a u8 <: &'b u8) => Subtype('b <: 'a) => Outlives('a: 'b) => RegionSubRegion('b <= 'a)` The reason for the current behavior is probably that we wanted `Subtype('b <: 'a)` and `RegionSubRegion('b <= 'a)` to be equivalent, but I don' t think this is a good reason since these relations are sufficiently different in that the first is a relation in the subtyping lattice and is intrinsic to the type-systems, while the the second relation is an implementation detail of regionck. This PR changes this behavior to use covariance, so.. `Subtype(&'a u8 <: &'b u8) => Subtype('a <: 'b) => Outlives('a: 'b) => RegionSubRegion('b <= 'a) ` Resolves #103676 r? `@lcnr`
2023-01-27Pacify tidy.Camille GILLOT-1/+1
2023-01-27Compute generator saved locals on MIR.Camille GILLOT-0/+8
2023-01-27yeetBoxy-88/+26
2023-01-27Introduce GeneratorWitnessMIR.Camille GILLOT-1/+3
2023-01-27Separate trait selection from ambiguity reporting.Camille GILLOT-3/+14
2023-01-26fixup new usages of fn_sig, bound_fn_sig after rebasingKyle Matsuda-5/+5
2023-01-26change fn_sig query to use EarlyBinder; remove bound_fn_sig query; add ↵Kyle Matsuda-5/+5
EarlyBinder to fn_sig in metadata
2023-01-26replace usages of fn_sig query with bound_fn_sigKyle Matsuda-1/+1
2023-01-27internally change regions to be covariantAli MJ Al-Nasrawy-7/+10
2023-01-26Intern CanonicalVarValuesMichael Goulet-18/+15
2023-01-26Rollup merge of #107304 - ↵Matthias Krüger-1/+657
Nilstrieb:ᐸTy as PartialEqᐳ::eq because what else are we gonna use in rustc_middle, r=compiler-errors Use `can_eq` to compare types for default assoc type error This correctly handles inference variables like `{integer}`. I had to move all of this `note_and_explain` code to `rustc_infer`, it made no sense for it to be in `rustc_middle` anyways. The commits are reviewed separately. Fixes #106968
2023-01-26improve fn pointer notesMatthew J Perez-8/+44
- add note and suggestion for casting both expected and found fn items to fn pointers - add note for casting expected fn item to fn pointer
2023-01-25Use `can_eq` to compare types for default assoc type errorNilstrieb-3/+9
This works correctly with inference variables.
2023-01-25Move `note_and_explain_type_err` from `rustc_middle` to `rustc_infer`Nilstrieb-1/+651
This way we can properly deal with the types.
2023-01-25Auto merge of #103902 - vincenzopalazzo:macros/obligation_rulesv2, r=oli-obkbors-14/+6
use `LocalDefId` instead of `HirId` in trait resolution to simplify the obligation clause resolution This commit introduces a refactoring suggested by `@lcnr` to simplify the obligation clause resolution. This is just the first PR that introduces a type of refactoring, but others PRs will follow this to introduce name changing to change from the variable name from `body_id` to something else. Fixes https://github.com/rust-lang/rust/issues/104827 Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> `@rustbot` r? `@lcnr`
2023-01-24Add suggestions for function pointersMatthew J Perez-2/+79
- On compiler-error's suggestion of moving this lower down the stack, along the path of `report_mismatched_types()`, which is used by `rustc_hir_analysis` and `rustc_hir_typeck`. - update ui tests, add test - add suggestions for references to fn pointers - modify `TypeErrCtxt::same_type_modulo_infer` to take `T: relate::Relate` instead of `Ty`
2023-01-23Point at specific field in struct literal when trait fulfillment failsNathan Fenner-24/+26
2023-01-23fix: use LocalDefId instead of HirId in trait resVincenzo Palazzo-14/+6
use LocalDefId instead of HirId in trait resolution to simplify the obligation clause resolution Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-01-23Rollup merge of #104926 - spastorino:calculate_diverging_fallback-cleanups, ↵Dylan DPC-3/+0
r=lcnr Move relationships from FulfillmentContext to Inherited r? `@lcnr`
2023-01-22Remove confusing 'while checking' note from opaque future type mismatchesMichael Goulet-46/+16
2023-01-22Store relationships on InherentSantiago Pastorino-3/+0
2023-01-22Rollup merge of #107111 - chenyukang:yukang/fix-107090-fluent-parameters, ↵Matthias Krüger-1/+6
r=petrochenkov Fix missing arguments issues and copy-paste bug for fluent Fixes #107090
2023-01-20Fix typo in opaque_types.rsIkko Eltociear Ashimine-2/+2
paramters -> parameters
2023-01-20Auto merge of #106090 - WaffleLapkin:dereffffffffff, r=Nilstriebbors-11/+11
Remove some `ref` patterns from the compiler Previous PR: https://github.com/rust-lang/rust/pull/105368 r? `@Nilstrieb`
2023-01-19even more unify Projection/Opaque in outlives codeAli MJ Al-Nasrawy-139/+65