Skip to content

Commit 60970be

Browse files
committed
Distinguish RPIT from other impl trait
1 parent 4af0952 commit 60970be

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/librustc/infer/opaque_types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
502502
// Otherwise, generate the label we'll use in the error message.
503503
hir::OpaqueTyOrigin::TypeAlias => "impl Trait",
504504
hir::OpaqueTyOrigin::FnReturn => "impl Trait",
505+
hir::OpaqueTyOrigin::Misc => "impl Trait",
505506
};
506507
let msg = format!("ambiguous lifetime bound in `{}`", context_name);
507508
let mut err = self.tcx.sess.struct_span_err(span, &msg);

src/librustc_ast_lowering/item.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
272272
let ty = self.lower_ty(
273273
t,
274274
if self.sess.features_untracked().impl_trait_in_bindings {
275-
ImplTraitContext::OpaqueTy(None)
275+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
276276
} else {
277277
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
278278
},
@@ -283,7 +283,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
283283
let ty = self.lower_ty(
284284
t,
285285
if self.sess.features_untracked().impl_trait_in_bindings {
286-
ImplTraitContext::OpaqueTy(None)
286+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
287287
} else {
288288
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
289289
},
@@ -327,8 +327,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
327327
}
328328
Some(bounds) => {
329329
let ty = hir::OpaqueTy {
330-
generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)),
331-
bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)),
330+
generics: self.lower_generics(
331+
generics,
332+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
333+
),
334+
bounds: self.lower_param_bounds(
335+
bounds,
336+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
337+
),
332338
impl_trait_fn: None,
333339
origin: hir::OpaqueTyOrigin::TypeAlias,
334340
};

src/librustc_ast_lowering/lib.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ enum ImplTraitContext<'b, 'a> {
222222
/// We optionally store a `DefId` for the parent item here so we can look up necessary
223223
/// information later. It is `None` when no information about the context should be stored
224224
/// (e.g., for consts and statics).
225-
OpaqueTy(Option<DefId> /* fn def-ID */),
225+
OpaqueTy(Option<DefId> /* fn def-ID */, hir::OpaqueTyOrigin),
226226

227227
/// `impl Trait` is not accepted in this position.
228228
Disallowed(ImplTraitPosition),
@@ -248,7 +248,7 @@ impl<'a> ImplTraitContext<'_, 'a> {
248248
use self::ImplTraitContext::*;
249249
match self {
250250
Universal(params) => Universal(params),
251-
OpaqueTy(fn_def_id) => OpaqueTy(*fn_def_id),
251+
OpaqueTy(fn_def_id, origin) => OpaqueTy(*fn_def_id, *origin),
252252
Disallowed(pos) => Disallowed(*pos),
253253
}
254254
}
@@ -1010,7 +1010,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10101010
// so desugar to
10111011
//
10121012
// fn foo() -> impl Iterator<Item = impl Debug>
1013-
ImplTraitContext::OpaqueTy(_) => (true, itctx),
1013+
ImplTraitContext::OpaqueTy(..) => (true, itctx),
10141014

10151015
// We are in the argument position, but within a dyn type:
10161016
//
@@ -1019,7 +1019,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10191019
// so desugar to
10201020
//
10211021
// fn foo(x: dyn Iterator<Item = impl Debug>)
1022-
ImplTraitContext::Universal(_) if self.is_in_dyn_type => (true, itctx),
1022+
ImplTraitContext::Universal(..) if self.is_in_dyn_type => (true, itctx),
10231023

10241024
// In `type Foo = dyn Iterator<Item: Debug>` we desugar to
10251025
// `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
@@ -1028,7 +1028,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10281028
//
10291029
// FIXME: this is only needed until `impl Trait` is allowed in type aliases.
10301030
ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => {
1031-
(true, ImplTraitContext::OpaqueTy(None))
1031+
(true, ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc))
10321032
}
10331033

10341034
// We are in the parameter position, but not within a dyn type:
@@ -1269,8 +1269,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12691269
TyKind::ImplTrait(def_node_id, ref bounds) => {
12701270
let span = t.span;
12711271
match itctx {
1272-
ImplTraitContext::OpaqueTy(fn_def_id) => {
1273-
self.lower_opaque_impl_trait(span, fn_def_id, def_node_id, |this| {
1272+
ImplTraitContext::OpaqueTy(fn_def_id, origin) => {
1273+
self.lower_opaque_impl_trait(span, fn_def_id, origin, def_node_id, |this| {
12741274
this.lower_param_bounds(bounds, itctx)
12751275
})
12761276
}
@@ -1349,6 +1349,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13491349
&mut self,
13501350
span: Span,
13511351
fn_def_id: Option<DefId>,
1352+
origin: hir::OpaqueTyOrigin,
13521353
opaque_ty_node_id: NodeId,
13531354
lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
13541355
) -> hir::TyKind<'hir> {
@@ -1390,7 +1391,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13901391
},
13911392
bounds: hir_bounds,
13921393
impl_trait_fn: fn_def_id,
1393-
origin: hir::OpaqueTyOrigin::FnReturn,
1394+
origin,
13941395
};
13951396

13961397
trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_index);
@@ -1622,7 +1623,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16221623
self.lower_ty(
16231624
t,
16241625
if self.sess.features_untracked().impl_trait_in_bindings {
1625-
ImplTraitContext::OpaqueTy(Some(parent_def_id))
1626+
ImplTraitContext::OpaqueTy(Some(parent_def_id), hir::OpaqueTyOrigin::Misc)
16261627
} else {
16271628
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
16281629
},
@@ -1723,9 +1724,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17231724
} else {
17241725
match decl.output {
17251726
FunctionRetTy::Ty(ref ty) => match in_band_ty_params {
1726-
Some((def_id, _)) if impl_trait_return_allow => hir::FunctionRetTy::Return(
1727-
self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(def_id))),
1728-
),
1727+
Some((def_id, _)) if impl_trait_return_allow => {
1728+
hir::FunctionRetTy::Return(self.lower_ty(
1729+
ty,
1730+
ImplTraitContext::OpaqueTy(Some(def_id), hir::OpaqueTyOrigin::FnReturn),
1731+
))
1732+
}
17291733
_ => hir::FunctionRetTy::Return(
17301734
self.lower_ty(ty, ImplTraitContext::disallowed()),
17311735
),
@@ -1957,7 +1961,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19571961
) -> hir::GenericBound<'hir> {
19581962
// Compute the `T` in `Future<Output = T>` from the return type.
19591963
let output_ty = match output {
1960-
FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))),
1964+
FunctionRetTy::Ty(ty) => self.lower_ty(
1965+
ty,
1966+
ImplTraitContext::OpaqueTy(Some(fn_def_id), hir::OpaqueTyOrigin::FnReturn),
1967+
),
19611968
FunctionRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
19621969
};
19631970

@@ -2102,9 +2109,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21022109
}
21032110

21042111
let kind = hir::GenericParamKind::Type {
2105-
default: default
2106-
.as_ref()
2107-
.map(|x| self.lower_ty(x, ImplTraitContext::OpaqueTy(None))),
2112+
default: default.as_ref().map(|x| {
2113+
self.lower_ty(
2114+
x,
2115+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
2116+
)
2117+
}),
21082118
synthetic: param
21092119
.attrs
21102120
.iter()

src/librustc_hir/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,8 @@ pub enum OpaqueTyOrigin {
19901990
FnReturn,
19911991
/// `async fn`
19921992
AsyncFn,
1993+
/// Impl trait in bindings, consts, statics, bounds.
1994+
Misc,
19931995
}
19941996

19951997
/// The various kinds of types recognized by the compiler.

0 commit comments

Comments
 (0)