Skip to content

Commit 0050715

Browse files
committed
allow method calls on opaques
1 parent d8a23f3 commit 0050715

28 files changed

+362
-165
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 181 additions & 28 deletions
Large diffs are not rendered by default.

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,32 @@ impl<'tcx> InferCtxt<'tcx> {
8585
where
8686
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
8787
{
88+
// While we ignore region constraints and pending obligations,
89+
// we do return constrained opaque types to avoid unconstrained
90+
// inference variables in the response. This is still slightly
91+
// insufficient as ambiguous `Projection` obligations have the
92+
// same issue.
93+
//
94+
// FIXME(-Znext-solver): We could alternatively extend the `var_values`
95+
// each time we call `make_query_response_ignoring_pending_obligations`
96+
// and equate inference variables created inside of the query this way.
97+
// This is what we do for `CanonicalState` and is probably a bit nicer.
98+
let opaque_types = if self.next_trait_solver() {
99+
self.inner
100+
.borrow_mut()
101+
.opaque_type_storage
102+
.iter_opaque_types()
103+
.map(|(k, v)| (k, v.ty))
104+
.collect()
105+
} else {
106+
vec![]
107+
};
108+
88109
self.canonicalize_response(QueryResponse {
89110
var_values: inference_vars,
90111
region_constraints: QueryRegionConstraints::default(),
91112
certainty: Certainty::Proven, // Ambiguities are OK!
92-
opaque_types: vec![],
113+
opaque_types,
93114
value: answer,
94115
})
95116
}

compiler/rustc_middle/src/traits/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'tcx> FromIterator<DropckConstraint<'tcx>> for DropckConstraint<'tcx> {
154154
#[derive(Debug, HashStable)]
155155
pub struct CandidateStep<'tcx> {
156156
pub self_ty: Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
157+
pub self_ty_is_opaque: bool,
157158
pub autoderefs: usize,
158159
/// `true` if the type results from a dereference of a raw pointer.
159160
/// when assembling candidates, we include these steps, but not when

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ where
194194
D: SolverDelegate<Interner = I>,
195195
I: Interner,
196196
{
197-
#[instrument(level = "debug", skip(self))]
197+
#[instrument(level = "debug", skip(self), ret)]
198198
fn evaluate_root_goal(
199199
&self,
200200
goal: Goal<I, I::Predicate>,
@@ -206,6 +206,7 @@ where
206206
})
207207
}
208208

209+
#[instrument(level = "debug", skip(self), ret)]
209210
fn root_goal_may_hold_opaque_types_jank(
210211
&self,
211212
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ where
381381
}
382382

383383
/// The result of evaluating a goal.
384+
#[derive_where(Debug; I: Interner)]
384385
pub struct GoalEvaluation<I: Interner> {
385386
/// The goal we've evaluated. This is the input goal, but potentially with its
386387
/// inference variables resolved. This never applies any inference constraints

compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_infer::traits::solve::Goal;
22
use rustc_macros::extension;
3-
use rustc_middle::span_bug;
3+
use rustc_middle::{span_bug, ty};
44
use rustc_next_trait_solver::solve::SolverDelegateEvalExt;
55

66
use crate::infer::InferCtxt;
@@ -22,7 +22,7 @@ impl<'tcx> InferCtxt<'tcx> {
2222
/// for more details.
2323
fn predicate_may_hold_opaque_types_jank(&self, obligation: &PredicateObligation<'tcx>) -> bool {
2424
if self.next_trait_solver() {
25-
<&SolverDelegate<'tcx>>::from(self).root_goal_may_hold_opaque_types_jank(Goal::new(
25+
self.goal_may_hold_opaque_types_jank(Goal::new(
2626
self.tcx,
2727
obligation.param_env,
2828
obligation.predicate,
@@ -32,6 +32,13 @@ impl<'tcx> InferCtxt<'tcx> {
3232
}
3333
}
3434

35+
/// See the comment on [OpaqueTypesJank](crate::solve::OpaqueTypesJank)
36+
/// for more details.
37+
fn goal_may_hold_opaque_types_jank(&self, goal: Goal<'tcx, ty::Predicate<'tcx>>) -> bool {
38+
assert!(self.next_trait_solver());
39+
<&SolverDelegate<'tcx>>::from(self).root_goal_may_hold_opaque_types_jank(goal)
40+
}
41+
3542
/// Evaluates whether the predicate can be satisfied in the given
3643
/// `ParamEnv`, and returns `false` if not certain. However, this is
3744
/// not entirely accurate if inference variables are involved.
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
error[E0282]: type annotations needed
1+
warning: variable does not need to be mutable
22
--> $DIR/call_method_ambiguous.rs:26:13
33
|
44
LL | let mut iter = foo(n - 1, m);
5-
| ^^^^^^^^
6-
LL |
7-
LL | assert_eq!(iter.get(), 1);
8-
| --- type must be known at this point
5+
| ----^^^^
6+
| |
7+
| help: remove this `mut`
98
|
10-
help: consider giving `iter` an explicit type
11-
|
12-
LL | let mut iter: /* Type */ = foo(n - 1, m);
13-
| ++++++++++++
9+
= note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
1410

15-
error: aborting due to 1 previous error
11+
warning: 1 warning emitted
1612

17-
For more information about this error, try `rustc --explain E0282`.

tests/ui/impl-trait/call_method_ambiguous.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: current next
22
//@[next] compile-flags: -Znext-solver
3-
//@[current] run-pass
3+
//@ run-pass
44

55
trait Get {
66
fn get(&mut self) -> u32;
@@ -24,7 +24,6 @@ where
2424
fn foo(n: usize, m: &mut ()) -> impl Get + use<'_> {
2525
if n > 0 {
2626
let mut iter = foo(n - 1, m);
27-
//[next]~^ ERROR type annotations needed
2827
assert_eq!(iter.get(), 1);
2928
}
3029
m

tests/ui/impl-trait/call_method_on_inherent_impl.next.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/ui/impl-trait/call_method_on_inherent_impl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: current next
22
//@[next] compile-flags: -Znext-solver
3-
//@[current] check-pass
3+
//@ check-pass
44

55
trait MyDebug {
66
fn my_debug(&self);
@@ -16,7 +16,6 @@ where
1616
fn my_foo() -> impl std::fmt::Debug {
1717
if false {
1818
let x = my_foo();
19-
//[next]~^ ERROR type annotations needed
2019
x.my_debug();
2120
}
2221
()

0 commit comments

Comments
 (0)