Skip to content

Commit 1dfe0e0

Browse files
committed
Remove one use of Bubble
1 parent bdb20f4 commit 1dfe0e0

11 files changed

+102
-78
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ fn check_opaque_type_well_formed<'tcx>(
293293
return Ok(definition_ty);
294294
};
295295
let param_env = tcx.param_env(def_id);
296-
// HACK This bubble is required for this tests to pass:
296+
// HACK We use the function's anchor, instead of the TAIT's `DefId`, because the
297+
// `TAIT` may not be in the defining scope of the other (nested) TAITs.
297298
// nested-return-type2-tait2.rs
298299
// nested-return-type2-tait3.rs
299300
// FIXME(-Ztrait-solver=next): We probably should use `DefiningAnchor::Error`
@@ -302,11 +303,7 @@ fn check_opaque_type_well_formed<'tcx>(
302303
let infcx = tcx
303304
.infer_ctxt()
304305
.with_next_trait_solver(next_trait_solver)
305-
.with_opaque_type_inference(if next_trait_solver {
306-
DefiningAnchor::Bind(def_id)
307-
} else {
308-
DefiningAnchor::Bubble
309-
})
306+
.with_opaque_type_inference(DefiningAnchor::Bind(def_id))
310307
.build();
311308
let ocx = ObligationCtxt::new(&infcx);
312309
let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir as hir;
22
use rustc_infer::infer::TyCtxtInferExt;
3-
use rustc_macros::{LintDiagnostic, Subdiagnostic};
3+
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
44
use rustc_middle::ty::{
55
self, fold::BottomUpFolder, print::TraitPredPrintModifiersAndPath, Ty, TypeFoldable,
66
};
@@ -140,25 +140,51 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
140140
}),
141141
_ => None,
142142
};
143-
cx.emit_spanned_lint(
144-
OPAQUE_HIDDEN_INFERRED_BOUND,
145-
pred_span,
146-
OpaqueHiddenInferredBoundLint {
147-
ty: cx.tcx.mk_opaque(
148-
def_id,
149-
ty::InternalSubsts::identity_for_item(cx.tcx, def_id),
150-
),
151-
proj_ty: proj_term,
152-
assoc_pred_span,
153-
add_bound,
154-
},
155-
);
143+
let ty = cx
144+
.tcx
145+
.mk_opaque(def_id, ty::InternalSubsts::identity_for_item(cx.tcx, def_id));
146+
match opaque.origin {
147+
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_) => {
148+
cx.emit_spanned_lint(
149+
OPAQUE_HIDDEN_INFERRED_BOUND,
150+
pred_span,
151+
OpaqueHiddenInferredBoundLint {
152+
ty,
153+
proj_ty: proj_term,
154+
assoc_pred_span,
155+
add_bound,
156+
},
157+
);
158+
}
159+
hir::OpaqueTyOrigin::TyAlias { .. } => {
160+
cx.tcx.sess.emit_err(OpaqueHiddenInferredBoundErr {
161+
pred_span,
162+
ty,
163+
proj_ty: proj_term,
164+
assoc_pred_span,
165+
add_bound,
166+
});
167+
}
168+
}
156169
}
157170
}
158171
}
159172
}
160173
}
161174

175+
#[derive(Diagnostic)]
176+
#[diag(lint_opaque_hidden_inferred_bound)]
177+
struct OpaqueHiddenInferredBoundErr<'tcx> {
178+
#[primary_span]
179+
pred_span: Span,
180+
ty: Ty<'tcx>,
181+
proj_ty: Ty<'tcx>,
182+
#[label(lint_specifically)]
183+
assoc_pred_span: Span,
184+
#[subdiagnostic]
185+
add_bound: Option<AddBound<'tcx>>,
186+
}
187+
162188
#[derive(LintDiagnostic)]
163189
#[diag(lint_opaque_hidden_inferred_bound)]
164190
struct OpaqueHiddenInferredBoundLint<'tcx> {

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,24 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
5050
}
5151

5252
fn parent(&self) -> Option<LocalDefId> {
53-
match self.tcx.def_kind(self.item) {
53+
let mut item = self.item;
54+
let mut kind;
55+
loop {
56+
kind = self.tcx.def_kind(item);
57+
match kind {
58+
DefKind::OpaqueTy => item = self.tcx.local_parent(item),
59+
DefKind::TyAlias => return None,
60+
_ => break,
61+
}
62+
}
63+
match kind {
5464
DefKind::Fn => None,
5565
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
56-
Some(self.tcx.local_parent(self.item))
66+
Some(self.tcx.local_parent(item))
67+
}
68+
other => {
69+
span_bug!(self.tcx.def_span(item), "unhandled item with opaque types: {other:?}")
5770
}
58-
other => span_bug!(
59-
self.tcx.def_span(self.item),
60-
"unhandled item with opaque types: {other:?}"
61-
),
6271
}
6372
}
6473
}
@@ -216,6 +225,18 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
216225
}
217226
tcx.arena.alloc_from_iter(collector.opaques)
218227
}
228+
DefKind::OpaqueTy => {
229+
let mut collector = OpaqueTypeCollector::new(tcx, item);
230+
let span = match tcx.hir().get_by_def_id(item).ty() {
231+
Some(ty) => ty.span,
232+
_ => tcx.def_span(item),
233+
};
234+
collector.visit_spanned(
235+
span,
236+
tcx.mk_opaque(item.to_def_id(), ty::InternalSubsts::identity_for_item(tcx, item)),
237+
);
238+
tcx.arena.alloc_from_iter(collector.opaques)
239+
}
219240
DefKind::Mod
220241
| DefKind::Struct
221242
| DefKind::Union
@@ -236,7 +257,6 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
236257
| DefKind::ForeignMod
237258
| DefKind::AnonConst
238259
| DefKind::InlineConst
239-
| DefKind::OpaqueTy
240260
| DefKind::ImplTraitPlaceholder
241261
| DefKind::Field
242262
| DefKind::LifetimeParam

tests/ui/impl-trait/nested-return-type2-tait2.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<R: Duh, F: FnMut() -> R> Trait for F {
1717

1818
type Sendable = impl Send;
1919
type Traitable = impl Trait<Assoc = Sendable>;
20+
//~^ ERROR opaque type `Traitable` does not satisfy its associated type bounds
2021

2122
// The `impl Send` here is then later compared against the inference var
2223
// created, causing the inference var to be set to `impl Send` instead of
@@ -25,8 +26,6 @@ type Traitable = impl Trait<Assoc = Sendable>;
2526
// type does not implement `Duh`, even if its hidden type does. So we error out.
2627
fn foo() -> Traitable {
2728
|| 42
28-
//~^ ERROR `Sendable: Duh` is not satisfied
2929
}
3030

31-
fn main() {
32-
}
31+
fn main() {}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
error[E0277]: the trait bound `Sendable: Duh` is not satisfied
2-
--> $DIR/nested-return-type2-tait2.rs:27:5
1+
error: opaque type `Traitable` does not satisfy its associated type bounds
2+
--> $DIR/nested-return-type2-tait2.rs:19:29
33
|
4-
LL | || 42
5-
| ^^^^^ the trait `Duh` is not implemented for `Sendable`
6-
|
7-
= help: the trait `Duh` is implemented for `i32`
8-
note: required for `[closure@$DIR/nested-return-type2-tait2.rs:27:5: 27:7]` to implement `Trait`
9-
--> $DIR/nested-return-type2-tait2.rs:14:31
10-
|
11-
LL | impl<R: Duh, F: FnMut() -> R> Trait for F {
12-
| --- ^^^^^ ^
13-
| |
14-
| unsatisfied trait bound introduced here
4+
LL | type Assoc: Duh;
5+
| --- this associated type bound is unsatisfied for `Sendable`
6+
...
7+
LL | type Traitable = impl Trait<Assoc = Sendable>;
8+
| ^^^^^^^^^^^^^^^^
159

1610
error: aborting due to previous error
1711

18-
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/nested-return-type2-tait3.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@ impl<R: Duh, F: FnMut() -> R> Trait for F {
1515
type Assoc = R;
1616
}
1717

18-
type Traitable = impl Trait<Assoc = impl Send>;
19-
2018
// The `impl Send` here is then later compared against the inference var
2119
// created, causing the inference var to be set to `impl Send` instead of
2220
// the hidden type. We already have obligations registered on the inference
2321
// var to make it uphold the `: Duh` bound on `Trait::Assoc`. The opaque
2422
// type does not implement `Duh`, even if its hidden type does. So we error out.
23+
type Traitable = impl Trait<Assoc = impl Send>;
24+
//~^ ERROR opaque type `Traitable` does not satisfy its associated type bounds
25+
2526
fn foo() -> Traitable {
2627
|| 42
27-
//~^ ERROR `impl Send: Duh` is not satisfied
2828
}
2929

30-
fn main() {
31-
}
30+
fn main() {}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
error[E0277]: the trait bound `impl Send: Duh` is not satisfied
2-
--> $DIR/nested-return-type2-tait3.rs:26:5
1+
error: opaque type `Traitable` does not satisfy its associated type bounds
2+
--> $DIR/nested-return-type2-tait3.rs:23:29
33
|
4-
LL | || 42
5-
| ^^^^^ the trait `Duh` is not implemented for `impl Send`
4+
LL | type Assoc: Duh;
5+
| --- this associated type bound is unsatisfied for `impl Send`
6+
...
7+
LL | type Traitable = impl Trait<Assoc = impl Send>;
8+
| ^^^^^^^^^^^^^^^^^
69
|
7-
= help: the trait `Duh` is implemented for `i32`
8-
note: required for `[closure@$DIR/nested-return-type2-tait3.rs:26:5: 26:7]` to implement `Trait`
9-
--> $DIR/nested-return-type2-tait3.rs:14:31
10+
help: add this bound
1011
|
11-
LL | impl<R: Duh, F: FnMut() -> R> Trait for F {
12-
| --- ^^^^^ ^
13-
| |
14-
| unsatisfied trait bound introduced here
12+
LL | type Traitable = impl Trait<Assoc = impl Send + Duh>;
13+
| +++++
1514

1615
error: aborting due to previous error
1716

18-
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/nested-return-type3-tait2.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// check-pass
2-
31
#![feature(type_alias_impl_trait)]
42

53
trait Duh {}
@@ -16,11 +14,10 @@ impl<F: Duh> Trait for F {
1614

1715
type Sendable = impl Send;
1816
type Traitable = impl Trait<Assoc = Sendable>;
19-
//~^ WARN opaque type `Traitable` does not satisfy its associated type bounds
17+
//~^ ERROR opaque type `Traitable` does not satisfy its associated type bounds
2018

2119
fn foo() -> Traitable {
2220
42
2321
}
2422

25-
fn main() {
26-
}
23+
fn main() {}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
warning: opaque type `Traitable` does not satisfy its associated type bounds
2-
--> $DIR/nested-return-type3-tait2.rs:18:29
1+
error: opaque type `Traitable` does not satisfy its associated type bounds
2+
--> $DIR/nested-return-type3-tait2.rs:16:29
33
|
44
LL | type Assoc: Duh;
55
| --- this associated type bound is unsatisfied for `Sendable`
66
...
77
LL | type Traitable = impl Trait<Assoc = Sendable>;
88
| ^^^^^^^^^^^^^^^^
9-
|
10-
= note: `#[warn(opaque_hidden_inferred_bound)]` on by default
119

12-
warning: 1 warning emitted
10+
error: aborting due to previous error
1311

tests/ui/impl-trait/nested-return-type3-tait3.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// check-pass
2-
31
#![feature(type_alias_impl_trait)]
42

53
trait Duh {}
@@ -15,7 +13,7 @@ impl<F: Duh> Trait for F {
1513
}
1614

1715
type Traitable = impl Trait<Assoc = impl Send>;
18-
//~^ WARN opaque type `Traitable` does not satisfy its associated type bounds
16+
//~^ ERROR opaque type `Traitable` does not satisfy its associated type bounds
1917

2018
fn foo() -> Traitable {
2119
42
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
warning: opaque type `Traitable` does not satisfy its associated type bounds
2-
--> $DIR/nested-return-type3-tait3.rs:17:29
1+
error: opaque type `Traitable` does not satisfy its associated type bounds
2+
--> $DIR/nested-return-type3-tait3.rs:15:29
33
|
44
LL | type Assoc: Duh;
55
| --- this associated type bound is unsatisfied for `impl Send`
66
...
77
LL | type Traitable = impl Trait<Assoc = impl Send>;
88
| ^^^^^^^^^^^^^^^^^
99
|
10-
= note: `#[warn(opaque_hidden_inferred_bound)]` on by default
1110
help: add this bound
1211
|
1312
LL | type Traitable = impl Trait<Assoc = impl Send + Duh>;
1413
| +++++
1514

16-
warning: 1 warning emitted
15+
error: aborting due to previous error
1716

0 commit comments

Comments
 (0)