Skip to content

Commit 45af29b

Browse files
committed
w
1 parent 0cf7b6a commit 45af29b

16 files changed

+174
-50
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_span::edit_distance::{
3030
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
3131
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
3232
use rustc_trait_selection::infer::InferCtxtExt as _;
33+
use rustc_trait_selection::solve::Goal;
3334
use rustc_trait_selection::traits::query::CanonicalMethodAutoderefStepsGoal;
3435
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
3536
use rustc_trait_selection::traits::query::method_autoderef::{
@@ -1872,7 +1873,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18721873
}
18731874
}
18741875

1875-
#[instrument(level = "trace", skip(self, possibly_unsatisfied_predicates), ret)]
1876+
#[instrument(level = "debug", skip(self, possibly_unsatisfied_predicates), ret)]
18761877
fn consider_probe(
18771878
&self,
18781879
self_ty: Ty<'tcx>,
@@ -1884,15 +1885,17 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18841885
Option<ObligationCause<'tcx>>,
18851886
)>,
18861887
) -> ProbeResult {
1887-
debug!("consider_probe: self_ty={:?} probe={:?}", self_ty, probe);
1888-
18891888
self.probe(|snapshot| {
18901889
let outer_universe = self.universe();
18911890

18921891
let mut result = ProbeResult::Match;
18931892
let cause = &self.misc(self.span);
18941893
let ocx = ObligationCtxt::new_with_diagnostics(self);
18951894
ocx.register_obligations(instantiate_self_ty_obligations.iter().cloned());
1895+
let errors = ocx.select_where_possible();
1896+
if !errors.is_empty() {
1897+
unreachable!("unexpected autoderef error {errors:?}");
1898+
}
18961899

18971900
let mut trait_predicate = None;
18981901
let (mut xform_self_ty, mut xform_ret_ty);
@@ -2104,6 +2107,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
21042107
}
21052108
}
21062109

2110+
if let Some(predicate) = trait_predicate
2111+
&& self.infcx.next_trait_solver()
2112+
{
2113+
let goal = Goal { param_env: self.param_env, predicate };
2114+
2115+
if !self.infcx.goal_may_hold_opaque_types_jank(goal) {
2116+
result = ProbeResult::NoMatch;
2117+
}
2118+
}
2119+
21072120
if let ProbeResult::Match = result
21082121
&& let Some(return_ty) = self.return_type
21092122
&& let Some(mut xform_ret_ty) = xform_ret_ty
@@ -2150,13 +2163,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
21502163
};
21512164
let ocx = ObligationCtxt::new(self);
21522165
let self_ty = ocx.register_infer_ok_obligations(ok);
2153-
if !ocx.select_all_or_error().is_empty() {
2154-
return false;
2166+
if ocx.select_all_or_error().is_empty() {
2167+
!self.resolve_vars_if_possible(self_ty).is_ty_var()
2168+
} else {
2169+
true
21552170
}
2156-
2157-
!self.resolve_vars_if_possible(self_ty).is_ty_var()
21582171
}) {
2172+
debug!("candidate constrains opaque --> reject");
21592173
result = ProbeResult::NoMatch;
2174+
break;
21602175
}
21612176
}
21622177
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,22 @@ impl<'tcx> InferCtxt<'tcx> {
8585
where
8686
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
8787
{
88+
let opaque_types = if self.next_trait_solver() {
89+
self.inner
90+
.borrow_mut()
91+
.opaque_type_storage
92+
.iter_opaque_types()
93+
.map(|(k, v)| (k, v.ty))
94+
.collect()
95+
} else {
96+
vec![]
97+
};
98+
8899
self.canonicalize_response(QueryResponse {
89100
var_values: inference_vars,
90101
region_constraints: QueryRegionConstraints::default(),
91102
certainty: Certainty::Proven, // Ambiguities are OK!
92-
opaque_types: vec![],
103+
opaque_types,
93104
value: answer,
94105
})
95106
}

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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
error[E0277]: the trait bound `(): MyDebug` is not satisfied
1+
error[E0599]: no method named `my_debug` found for reference `&_` in the current scope
22
--> $DIR/call_method_on_inherent_impl_on_rigid_type.rs:15:11
33
|
44
LL | x.my_debug();
5-
| ^^^^^^^^ the trait `MyDebug` is not implemented for `()`
5+
| ^^^^^^^^ method not found in `&_`
6+
|
7+
= help: items from traits can only be used if the trait is implemented and in scope
8+
help: trait `MyDebug` which provides `my_debug` is implemented but not in scope; perhaps you want to import it
9+
|
10+
LL + use MyDebug;
611
|
7-
= help: the trait `MyDebug` is implemented for `&()`
812

913
error: aborting due to 1 previous error
1014

11-
For more information about this error, try `rustc --explain E0277`.
15+
For more information about this error, try `rustc --explain E0599`.

tests/ui/impl-trait/call_method_on_inherent_impl_on_rigid_type.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ fn my_foo() -> impl std::fmt::Debug {
1313
if false {
1414
let x = &my_foo();
1515
x.my_debug();
16-
//[current]~^ ERROR: no method named `my_debug`
17-
//[next]~^^ ERROR: the trait bound `(): MyDebug` is not satisfied
16+
//~^ ERROR: no method named `my_debug`
1817
}
1918
()
2019
}

tests/ui/impl-trait/call_method_on_inherent_impl_ref.current.stderr renamed to tests/ui/impl-trait/call_method_on_inherent_impl_ref-err.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0599]: no method named `my_debug` found for opaque type `impl Debug` in the current scope
2-
--> $DIR/call_method_on_inherent_impl_ref.rs:18:11
2+
--> $DIR/call_method_on_inherent_impl_ref-err.rs:18:11
33
|
44
LL | fn my_debug(&self);
55
| -------- the method is available for `&impl Debug` here
@@ -9,7 +9,7 @@ LL | x.my_debug();
99
|
1010
= help: items from traits can only be used if the trait is implemented and in scope
1111
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
12-
--> $DIR/call_method_on_inherent_impl_ref.rs:4:1
12+
--> $DIR/call_method_on_inherent_impl_ref-err.rs:4:1
1313
|
1414
LL | trait MyDebug {
1515
| ^^^^^^^^^^^^^
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0599]: no method named `my_debug` found for type `_` in the current scope
2+
--> $DIR/call_method_on_inherent_impl_ref-err.rs:18:11
3+
|
4+
LL | fn my_debug(&self);
5+
| -------- the method is available for `&_` here
6+
...
7+
LL | x.my_debug();
8+
| ^^^^^^^^ method not found in `_`
9+
|
10+
= help: items from traits can only be used if the trait is implemented and in scope
11+
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
12+
--> $DIR/call_method_on_inherent_impl_ref-err.rs:4:1
13+
|
14+
LL | trait MyDebug {
15+
| ^^^^^^^^^^^^^
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0599`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ revisions: current next
2+
//@[next] compile-flags: -Znext-solver
3+
4+
trait MyDebug {
5+
fn my_debug(&self);
6+
}
7+
8+
impl<T> MyDebug for &T
9+
where
10+
T: std::fmt::Debug,
11+
{
12+
fn my_debug(&self) {}
13+
}
14+
15+
fn my_foo() -> impl std::fmt::Debug {
16+
if false {
17+
let x = my_foo();
18+
x.my_debug();
19+
//~^ ERROR no method named `my_debug` found
20+
}
21+
()
22+
}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)