Skip to content

Commit 55df920

Browse files
Tweak feature error, add test
1 parent 05812df commit 55df920

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl std::fmt::Display for ImplTraitPosition {
312312
}
313313
}
314314

315-
#[derive(Debug)]
315+
#[derive(Debug, PartialEq, Eq)]
316316
enum FnDeclKind {
317317
Fn,
318318
Inherent,
@@ -1373,6 +1373,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13731373
}
13741374
path
13751375
}
1376+
ImplTraitContext::Disallowed(
1377+
position @ (ImplTraitPosition::TraitReturn | ImplTraitPosition::ImplReturn),
1378+
) => {
1379+
self.tcx.sess.create_feature_err(
1380+
MisplacedImplTrait {
1381+
span: t.span,
1382+
position: DiagnosticArgFromDisplay(&position),
1383+
},
1384+
sym::return_position_impl_trait_in_trait,
1385+
).emit();
1386+
hir::TyKind::Err
1387+
}
13761388
ImplTraitContext::Disallowed(position) => {
13771389
self.tcx.sess.emit_err(MisplacedImplTrait {
13781390
span: t.span,
@@ -1717,13 +1729,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17171729
}
17181730
_ => {
17191731
if !kind.impl_trait_return_allowed(self.tcx) {
1720-
self.tcx
1721-
.sess
1722-
.create_feature_err(
1723-
TraitFnAsync { fn_span, span },
1724-
sym::return_position_impl_trait_in_trait,
1725-
)
1726-
.emit();
1732+
if kind == FnDeclKind::Impl {
1733+
self.tcx
1734+
.sess
1735+
.create_feature_err(
1736+
TraitFnAsync { fn_span, span },
1737+
sym::return_position_impl_trait_in_trait,
1738+
)
1739+
.emit();
1740+
} else {
1741+
self.tcx.sess.emit_err(TraitFnAsync { fn_span, span });
1742+
}
17271743
}
17281744
self.lower_async_fn_ret_ty(
17291745
&decl.output,

src/test/ui/async-await/async-trait-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ LL | async fn bar(&self) {}
6262
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
6363
| ------------------------------- the found opaque type
6464
|
65-
= note: expected associated type `<Self as T>::bar::{opaque#0}`
65+
= note: expected associated type `<Self as T>::bar::{opaque#0}<'_>`
6666
found opaque type `impl Future<Output = ()>`
6767

6868
error[E0308]: mismatched types

src/test/ui/feature-gates/feature-gate-return_position_impl_trait_in_trait.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
33
|
44
LL | fn bar() -> impl Sized;
55
| ^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable
69

710
error: aborting due to previous error
811

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
trait Foo {
7+
fn f() -> Box<impl Sized>;
8+
}
9+
10+
impl Foo for () {
11+
fn f() -> Box<String> {
12+
Box::new(String::new())
13+
}
14+
}
15+
16+
fn main() {
17+
let x: Box<String> = <() as Foo>::f();
18+
}

src/test/ui/impl-trait/where-allowed.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,18 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
162162
|
163163
LL | fn in_return() -> impl Debug;
164164
| ^^^^^^^^^^
165+
|
166+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
167+
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable
165168

166169
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `impl` method return
167170
--> $DIR/where-allowed.rs:125:34
168171
|
169172
LL | fn in_trait_impl_return() -> impl Debug { () }
170173
| ^^^^^^^^^^
174+
|
175+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
176+
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable
171177

172178
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `extern fn` param
173179
--> $DIR/where-allowed.rs:138:33

0 commit comments

Comments
 (0)