Skip to content

Commit 7054245

Browse files
committed
Distinguish RPIT from other impl trait
1 parent 83419e9 commit 7054245

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
@@ -504,6 +504,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
504504
// Otherwise, generate the label we'll use in the error message.
505505
hir::OpaqueTyOrigin::TypeAlias => "impl Trait",
506506
hir::OpaqueTyOrigin::FnReturn => "impl Trait",
507+
hir::OpaqueTyOrigin::Misc => "impl Trait",
507508
};
508509
let msg = format!("ambiguous lifetime bound in `{}`", context_name);
509510
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
@@ -273,7 +273,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
273273
let ty = self.lower_ty(
274274
t,
275275
if self.sess.features_untracked().impl_trait_in_bindings {
276-
ImplTraitContext::OpaqueTy(None)
276+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
277277
} else {
278278
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
279279
},
@@ -284,7 +284,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284284
let ty = self.lower_ty(
285285
t,
286286
if self.sess.features_untracked().impl_trait_in_bindings {
287-
ImplTraitContext::OpaqueTy(None)
287+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
288288
} else {
289289
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
290290
},
@@ -331,8 +331,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
331331
}
332332
Some(bounds) => {
333333
let ty = hir::OpaqueTy {
334-
generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)),
335-
bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)),
334+
generics: self.lower_generics(
335+
generics,
336+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
337+
),
338+
bounds: self.lower_param_bounds(
339+
bounds,
340+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
341+
),
336342
impl_trait_fn: None,
337343
origin: hir::OpaqueTyOrigin::TypeAlias,
338344
};

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
}
@@ -1021,7 +1021,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10211021
// so desugar to
10221022
//
10231023
// fn foo() -> impl Iterator<Item = impl Debug>
1024-
ImplTraitContext::OpaqueTy(_) => (true, itctx),
1024+
ImplTraitContext::OpaqueTy(..) => (true, itctx),
10251025

10261026
// We are in the argument position, but within a dyn type:
10271027
//
@@ -1030,7 +1030,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10301030
// so desugar to
10311031
//
10321032
// fn foo(x: dyn Iterator<Item = impl Debug>)
1033-
ImplTraitContext::Universal(_) if self.is_in_dyn_type => (true, itctx),
1033+
ImplTraitContext::Universal(..) if self.is_in_dyn_type => (true, itctx),
10341034

10351035
// In `type Foo = dyn Iterator<Item: Debug>` we desugar to
10361036
// `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
@@ -1039,7 +1039,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10391039
//
10401040
// FIXME: this is only needed until `impl Trait` is allowed in type aliases.
10411041
ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => {
1042-
(true, ImplTraitContext::OpaqueTy(None))
1042+
(true, ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc))
10431043
}
10441044

10451045
// We are in the parameter position, but not within a dyn type:
@@ -1274,8 +1274,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12741274
TyKind::ImplTrait(def_node_id, ref bounds) => {
12751275
let span = t.span;
12761276
match itctx {
1277-
ImplTraitContext::OpaqueTy(fn_def_id) => {
1278-
self.lower_opaque_impl_trait(span, fn_def_id, def_node_id, |this| {
1277+
ImplTraitContext::OpaqueTy(fn_def_id, origin) => {
1278+
self.lower_opaque_impl_trait(span, fn_def_id, origin, def_node_id, |this| {
12791279
this.lower_param_bounds(bounds, itctx)
12801280
})
12811281
}
@@ -1354,6 +1354,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13541354
&mut self,
13551355
span: Span,
13561356
fn_def_id: Option<DefId>,
1357+
origin: hir::OpaqueTyOrigin,
13571358
opaque_ty_node_id: NodeId,
13581359
lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
13591360
) -> hir::TyKind<'hir> {
@@ -1395,7 +1396,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13951396
},
13961397
bounds: hir_bounds,
13971398
impl_trait_fn: fn_def_id,
1398-
origin: hir::OpaqueTyOrigin::FnReturn,
1399+
origin,
13991400
};
14001401

14011402
trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_index);
@@ -1627,7 +1628,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16271628
self.lower_ty(
16281629
t,
16291630
if self.sess.features_untracked().impl_trait_in_bindings {
1630-
ImplTraitContext::OpaqueTy(Some(parent_def_id))
1631+
ImplTraitContext::OpaqueTy(Some(parent_def_id), hir::OpaqueTyOrigin::Misc)
16311632
} else {
16321633
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
16331634
},
@@ -1728,9 +1729,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17281729
} else {
17291730
match decl.output {
17301731
FunctionRetTy::Ty(ref ty) => match in_band_ty_params {
1731-
Some((def_id, _)) if impl_trait_return_allow => hir::FunctionRetTy::Return(
1732-
self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(def_id))),
1733-
),
1732+
Some((def_id, _)) if impl_trait_return_allow => {
1733+
hir::FunctionRetTy::Return(self.lower_ty(
1734+
ty,
1735+
ImplTraitContext::OpaqueTy(Some(def_id), hir::OpaqueTyOrigin::FnReturn),
1736+
))
1737+
}
17341738
_ => hir::FunctionRetTy::Return(
17351739
self.lower_ty(ty, ImplTraitContext::disallowed()),
17361740
),
@@ -1962,7 +1966,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19621966
) -> hir::GenericBound<'hir> {
19631967
// Compute the `T` in `Future<Output = T>` from the return type.
19641968
let output_ty = match output {
1965-
FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))),
1969+
FunctionRetTy::Ty(ty) => self.lower_ty(
1970+
ty,
1971+
ImplTraitContext::OpaqueTy(Some(fn_def_id), hir::OpaqueTyOrigin::FnReturn),
1972+
),
19661973
FunctionRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
19671974
};
19681975

@@ -2107,9 +2114,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21072114
}
21082115

21092116
let kind = hir::GenericParamKind::Type {
2110-
default: default
2111-
.as_ref()
2112-
.map(|x| self.lower_ty(x, ImplTraitContext::OpaqueTy(None))),
2117+
default: default.as_ref().map(|x| {
2118+
self.lower_ty(
2119+
x,
2120+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
2121+
)
2122+
}),
21132123
synthetic: param
21142124
.attrs
21152125
.iter()

src/librustc_hir/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,8 @@ pub enum OpaqueTyOrigin {
19711971
FnReturn,
19721972
/// `async fn`
19731973
AsyncFn,
1974+
/// Impl trait in bindings, consts, statics, bounds.
1975+
Misc,
19741976
}
19751977

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

0 commit comments

Comments
 (0)