Skip to content

Commit f10af2e

Browse files
committed
Translate zero-sized return types as void
The only way to get a value for a zero-sized type is `undef`, so there's really no point in actually having a return type other than void for such types. Also, while the comment in return_type_is_void mentioned something about aiding C ABI support, @eddyb correctly pointed out on IRC that there is no such thing as a zero-sized type in C. And even with clang, which allows empty structs, those get translated as void return types as well. Fixes #28766
1 parent a70a60a commit f10af2e

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/librustc_trans/trans/common.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -
119119
}
120120

121121
/// Identifies types which we declare to be equivalent to `void` in C for the purpose of function
122-
/// return types. These are `()`, bot, and uninhabited enums. Note that all such types are also
123-
/// zero-size, but not all zero-size types use a `void` return type (in order to aid with C ABI
124-
/// compatibility).
125-
pub fn return_type_is_void(ccx: &CrateContext, ty: Ty) -> bool {
126-
ty.is_nil() || ty.is_empty(ccx.tcx())
122+
/// return types. These are `()`, bot, uninhabited enums and all other zero-sized types.
123+
pub fn return_type_is_void<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
124+
ty.is_nil() || ty.is_empty(ccx.tcx()) || type_is_zero_size(ccx, ty)
127125
}
128126

129127
/// Generates a unique symbol based off the name given. This is used to create
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -O foo.rs
5+
$(RUSTC) -O -L $(TMPDIR) main.rs

src/test/run-make/issue-28766/foo.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type="lib"]
12+
pub struct Foo(());
13+
14+
impl Foo {
15+
pub fn new() -> Foo {
16+
Foo(())
17+
}
18+
}

src/test/run-make/issue-28766/main.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type="lib"]
12+
extern crate foo;
13+
use foo::Foo;
14+
15+
pub fn crash() -> Box<Foo> {
16+
Box::new(Foo::new())
17+
}

0 commit comments

Comments
 (0)