diff options
| author | Björn Steinbrink <bsteinbr@gmail.com> | 2016-01-15 17:34:38 +0100 |
|---|---|---|
| committer | Björn Steinbrink <bsteinbr@gmail.com> | 2016-01-15 18:29:29 +0100 |
| commit | f10af2e5fa505a217f7b22591b29953e9c75d835 (patch) | |
| tree | 116fe8906c676f8fdf0d5edb6618d91df27d1ed2 | |
| parent | a70a60a02bca4c1fb75a0eb0ecf80acb73972f66 (diff) | |
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
| -rw-r--r-- | src/librustc_trans/trans/common.rs | 8 | ||||
| -rw-r--r-- | src/test/run-make/issue-28766/Makefile | 5 | ||||
| -rw-r--r-- | src/test/run-make/issue-28766/foo.rs | 18 | ||||
| -rw-r--r-- | src/test/run-make/issue-28766/main.rs | 17 |
4 files changed, 43 insertions, 5 deletions
diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs index 5046c2e2920..2154e33f1a6 100644 --- a/src/librustc_trans/trans/common.rs +++ b/src/librustc_trans/trans/common.rs @@ -119,11 +119,9 @@ pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) - } /// Identifies types which we declare to be equivalent to `void` in C for the purpose of function -/// return types. These are `()`, bot, and uninhabited enums. Note that all such types are also -/// zero-size, but not all zero-size types use a `void` return type (in order to aid with C ABI -/// compatibility). -pub fn return_type_is_void(ccx: &CrateContext, ty: Ty) -> bool { - ty.is_nil() || ty.is_empty(ccx.tcx()) +/// return types. These are `()`, bot, uninhabited enums and all other zero-sized types. +pub fn return_type_is_void<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool { + ty.is_nil() || ty.is_empty(ccx.tcx()) || type_is_zero_size(ccx, ty) } /// Generates a unique symbol based off the name given. This is used to create diff --git a/src/test/run-make/issue-28766/Makefile b/src/test/run-make/issue-28766/Makefile new file mode 100644 index 00000000000..1f47ef15b27 --- /dev/null +++ b/src/test/run-make/issue-28766/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTC) -O foo.rs + $(RUSTC) -O -L $(TMPDIR) main.rs diff --git a/src/test/run-make/issue-28766/foo.rs b/src/test/run-make/issue-28766/foo.rs new file mode 100644 index 00000000000..3ed0a6bfc74 --- /dev/null +++ b/src/test/run-make/issue-28766/foo.rs @@ -0,0 +1,18 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type="lib"] +pub struct Foo(()); + +impl Foo { + pub fn new() -> Foo { + Foo(()) + } +} diff --git a/src/test/run-make/issue-28766/main.rs b/src/test/run-make/issue-28766/main.rs new file mode 100644 index 00000000000..d1dadbdc7ad --- /dev/null +++ b/src/test/run-make/issue-28766/main.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type="lib"] +extern crate foo; +use foo::Foo; + +pub fn crash() -> Box<Foo> { + Box::new(Foo::new()) +} |
