Skip to content

Commit 9cc1179

Browse files
committedNov 23, 2021
Fix printing unit return ty, don't elaborate FnOnce unless we see it
1 parent 9ae575c commit 9cc1179

11 files changed

+40
-31
lines changed
 

‎compiler/rustc_middle/src/ty/print/pretty.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -839,19 +839,25 @@ pub trait PrettyPrinter<'tcx>:
839839
p!(print(ty));
840840
}
841841

842-
p!(") -> ", print(return_ty), write("{}", if paren_needed { ")" } else { "" }));
842+
p!(")");
843+
if !return_ty.skip_binder().is_unit() {
844+
p!("-> ", print(return_ty));
845+
}
846+
p!(write("{}", if paren_needed { ")" } else { "" }));
843847

844848
first = false;
845849
}
846850
// If we got here, we can't print as a `impl Fn(A, B) -> C`. Just record the
847851
// trait_refs we collected in the OpaqueFnEntry as normal trait refs.
848852
_ => {
849-
traits.entry(fn_once_trait_ref).or_default().extend(
850-
// Group the return ty with its def id, if we had one.
851-
entry
852-
.return_ty
853-
.map(|ty| (self.tcx().lang_items().fn_once_output().unwrap(), ty)),
854-
);
853+
if entry.has_fn_once {
854+
traits.entry(fn_once_trait_ref).or_default().extend(
855+
// Group the return ty with its def id, if we had one.
856+
entry
857+
.return_ty
858+
.map(|ty| (self.tcx().lang_items().fn_once_output().unwrap(), ty)),
859+
);
860+
}
855861
if let Some(trait_ref) = entry.fn_mut_trait_ref {
856862
traits.entry(trait_ref).or_default();
857863
}
@@ -943,6 +949,7 @@ pub trait PrettyPrinter<'tcx>:
943949
if let Some((_, ty)) = proj_ty {
944950
entry.return_ty = Some(ty);
945951
}
952+
entry.has_fn_once = true;
946953
return;
947954
} else if Some(trait_def_id) == self.tcx().lang_items().fn_mut_trait() {
948955
let super_trait_ref = crate::traits::util::supertraits(self.tcx(), trait_ref)
@@ -2695,6 +2702,8 @@ pub fn provide(providers: &mut ty::query::Providers) {
26952702

26962703
#[derive(Default)]
26972704
pub struct OpaqueFnEntry<'tcx> {
2705+
// The trait ref is already stored as a key, so just track if we have it as a real predicate
2706+
has_fn_once: bool,
26982707
fn_mut_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
26992708
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
27002709
return_ty: Option<ty::Binder<'tcx, Ty<'tcx>>>,

‎src/test/mir-opt/inline/issue_78442.bar.Inline.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
+ _4 = hide_foo() -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15
2020
// mir::Constant
2121
// + span: $DIR/issue-78442.rs:11:5: 11:13
22-
// + literal: Const { ty: fn() -> impl Fn() -> () {hide_foo}, val: Value(Scalar(<ZST>)) }
22+
// + literal: Const { ty: fn() -> impl Fn() {hide_foo}, val: Value(Scalar(<ZST>)) }
2323
}
2424

2525
bb1: {
2626
_3 = &_4; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15
2727
StorageLive(_5); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
28-
- _2 = <impl Fn() -> () as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
28+
- _2 = <impl Fn() as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
2929
- // mir::Constant
3030
- // + span: $DIR/issue-78442.rs:11:5: 11:15
31-
- // + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl Fn() -> (), ()) -> <impl Fn() -> () as std::ops::FnOnce<()>>::Output {<impl Fn() -> () as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) }
31+
- // + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl Fn(), ()) -> <impl Fn() as std::ops::FnOnce<()>>::Output {<impl Fn() as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) }
3232
+ _2 = move (*_3)() -> [return: bb5, unwind: bb3]; // scope 1 at $DIR/issue-78442.rs:11:5: 11:17
3333
}
3434

‎src/test/mir-opt/inline/issue_78442.bar.RevealAll.diff

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
debug _baz => _1; // in scope 0 at $DIR/issue-78442.rs:9:5: 9:9
66
let mut _0: (); // return place in scope 0 at $DIR/issue-78442.rs:10:3: 10:3
77
let _2: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17
8-
- let mut _3: &impl Fn() -> (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
9-
- let _4: impl Fn() -> (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
8+
- let mut _3: &impl Fn(); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
9+
- let _4: impl Fn(); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
1010
+ let mut _3: &fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
1111
+ let _4: fn() {foo}; // in scope 0 at $DIR/issue-78442.rs:11:5: 11:15
1212
let mut _5: (); // in scope 0 at $DIR/issue-78442.rs:11:5: 11:17
@@ -18,17 +18,17 @@
1818
_4 = hide_foo() -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15
1919
// mir::Constant
2020
// + span: $DIR/issue-78442.rs:11:5: 11:13
21-
// + literal: Const { ty: fn() -> impl Fn() -> () {hide_foo}, val: Value(Scalar(<ZST>)) }
21+
// + literal: Const { ty: fn() -> impl Fn() {hide_foo}, val: Value(Scalar(<ZST>)) }
2222
}
2323

2424
bb1: {
2525
_3 = &_4; // scope 0 at $DIR/issue-78442.rs:11:5: 11:15
2626
StorageLive(_5); // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
2727
nop; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
28-
_2 = <impl Fn() -> () as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
28+
_2 = <impl Fn() as Fn<()>>::call(move _3, move _5) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-78442.rs:11:5: 11:17
2929
// mir::Constant
3030
// + span: $DIR/issue-78442.rs:11:5: 11:15
31-
// + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl Fn() -> (), ()) -> <impl Fn() -> () as std::ops::FnOnce<()>>::Output {<impl Fn() -> () as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) }
31+
// + literal: Const { ty: for<'r> extern "rust-call" fn(&'r impl Fn(), ()) -> <impl Fn() as std::ops::FnOnce<()>>::Output {<impl Fn() as std::ops::Fn<()>>::call}, val: Value(Scalar(<ZST>)) }
3232
}
3333

3434
bb2: {

‎src/test/ui/entry-point/imported_main_const_fn_item_type_forbidden.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL | type MainFn = impl Fn();
2121
LL | pub const BAR: MainFn = bar;
2222
| ^^^ expected opaque type, found fn item
2323
|
24-
= note: expected opaque type `impl Fn() -> ()`
24+
= note: expected opaque type `impl Fn()`
2525
found fn item `fn() {bar}`
2626

2727
error: could not find defining uses

‎src/test/ui/impl-trait/auto-trait-leak2.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ error[E0277]: `Rc<Cell<i32>>` cannot be sent between threads safely
22
--> $DIR/auto-trait-leak2.rs:13:10
33
|
44
LL | fn before() -> impl Fn(i32) {
5-
| ------------ within this `impl Fn(i32) -> ()`
5+
| ------------ within this `impl Fn(i32)`
66
...
77
LL | send(before());
88
| ---- ^^^^^^^^ `Rc<Cell<i32>>` cannot be sent between threads safely
99
| |
1010
| required by a bound introduced by this call
1111
|
12-
= help: within `impl Fn(i32) -> ()`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
12+
= help: within `impl Fn(i32)`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
1313
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:7:5: 7:22]`
14-
= note: required because it appears within the type `impl Fn(i32) -> ()`
14+
= note: required because it appears within the type `impl Fn(i32)`
1515
note: required by a bound in `send`
1616
--> $DIR/auto-trait-leak2.rs:10:12
1717
|
@@ -27,11 +27,11 @@ LL | send(after());
2727
| required by a bound introduced by this call
2828
...
2929
LL | fn after() -> impl Fn(i32) {
30-
| ------------ within this `impl Fn(i32) -> ()`
30+
| ------------ within this `impl Fn(i32)`
3131
|
32-
= help: within `impl Fn(i32) -> ()`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
32+
= help: within `impl Fn(i32)`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
3333
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak2.rs:24:5: 24:22]`
34-
= note: required because it appears within the type `impl Fn(i32) -> ()`
34+
= note: required because it appears within the type `impl Fn(i32)`
3535
note: required by a bound in `send`
3636
--> $DIR/auto-trait-leak2.rs:10:12
3737
|

‎src/test/ui/impl-trait/issue-87450.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ LL | fn foo() -> impl Fn() {
1717
| ^^^^^^^^^ recursive opaque type
1818
...
1919
LL | wrap(wrap(wrap(wrap(wrap(wrap(wrap(foo())))))))
20-
| ----------------------------------------------- returning here with type `impl Fn() -> ()`
20+
| ----------------------------------------------- returning here with type `impl Fn()`
2121
...
2222
LL | fn wrap(f: impl Fn()) -> impl Fn() {
23-
| --------- returning this opaque type `impl Fn() -> ()`
23+
| --------- returning this opaque type `impl Fn()`
2424

2525
error: aborting due to previous error; 1 warning emitted
2626

‎src/test/ui/impl-trait/issues/issue-86201.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL |
77
LL | static STATIC_FN: FunType = some_fn;
88
| ^^^^^^^ expected opaque type, found fn item
99
|
10-
= note: expected opaque type `impl Fn<()> + FnOnce<()>`
10+
= note: expected opaque type `impl Fn<()>`
1111
found fn item `fn() {some_fn}`
1212

1313
error: could not find defining uses

‎src/test/ui/issues/issue-59494.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919
let g = |(a, _)| a;
2020
let t7 = |env| |a| |b| t7p(f, g)(((env, a), b));
2121
let t8 = t8n(t7, t7p(f, g));
22-
//~^ ERROR: expected a `Fn<(_,)>` closure, found `impl Fn(((_, _), _)) -> ()` [E0277]
22+
//~^ ERROR: expected a `Fn<(_,)>` closure, found `impl Fn(((_, _), _))` [E0277]
2323
}

‎src/test/ui/issues/issue-59494.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0277]: expected a `Fn<(_,)>` closure, found `impl Fn(((_, _), _)) -> ()`
1+
error[E0277]: expected a `Fn<(_,)>` closure, found `impl Fn(((_, _), _))`
22
--> $DIR/issue-59494.rs:21:22
33
|
44
LL | let t8 = t8n(t7, t7p(f, g));
5-
| --- ^^^^^^^^^ expected an `Fn<(_,)>` closure, found `impl Fn(((_, _), _)) -> ()`
5+
| --- ^^^^^^^^^ expected an `Fn<(_,)>` closure, found `impl Fn(((_, _), _))`
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `Fn<(_,)>` is not implemented for `impl Fn(((_, _), _)) -> ()`
9+
= help: the trait `Fn<(_,)>` is not implemented for `impl Fn(((_, _), _))`
1010
note: required by a bound in `t8n`
1111
--> $DIR/issue-59494.rs:5:45
1212
|

‎src/test/ui/lint/opaque-ty-ffi-unsafe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn ret_closure() -> A {
99

1010
extern "C" {
1111
pub fn a(_: A);
12-
//~^ ERROR `extern` block uses type `impl Fn() -> ()`, which is not FFI-safe [improper_ctypes]
12+
//~^ ERROR `extern` block uses type `impl Fn()`, which is not FFI-safe [improper_ctypes]
1313
}
1414

1515
fn main() {}

‎src/test/ui/lint/opaque-ty-ffi-unsafe.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `extern` block uses type `impl Fn() -> ()`, which is not FFI-safe
1+
error: `extern` block uses type `impl Fn()`, which is not FFI-safe
22
--> $DIR/opaque-ty-ffi-unsafe.rs:11:17
33
|
44
LL | pub fn a(_: A);

0 commit comments

Comments
 (0)
Please sign in to comment.