Skip to content

Commit ecce274

Browse files
committed
use if let to avoid potential div by zero
remove semicolon -_- Add rem_bytes to conditional to avoid error when performing mod by 0 Add test file to confirm compilation passes. Ensure we don't divide or mod by zero in llvm_type. Include test file from issue.
1 parent df40e61 commit ecce274

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/librustc_codegen_llvm/abi.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ impl LlvmType for Reg {
127127
impl LlvmType for CastTarget {
128128
fn llvm_type(&self, cx: &CodegenCx) -> Type {
129129
let rest_ll_unit = self.rest.unit.llvm_type(cx);
130-
let rest_count = self.rest.total.bytes() / self.rest.unit.size.bytes();
131-
let rem_bytes = self.rest.total.bytes() % self.rest.unit.size.bytes();
130+
let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
131+
(0, 0)
132+
} else {
133+
(self.rest.total.bytes() / self.rest.unit.size.bytes(),
134+
self.rest.total.bytes() % self.rest.unit.size.bytes())
135+
};
132136

133137
if self.prefix.iter().all(|x| x.is_none()) {
134138
// Simplify to a single unit when there is no prefix and size <= unit size

src/test/ui/issue-50761.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017 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+
// Confirm that we don't accidently divide or mod by zero in llvm_type
12+
13+
// compile-pass
14+
15+
#![feature(test)]
16+
17+
mod a {
18+
pub trait A {}
19+
}
20+
21+
mod b {
22+
pub struct Builder {}
23+
24+
pub fn new() -> Builder {
25+
Builder {}
26+
}
27+
28+
impl Builder {
29+
pub fn with_a(&mut self, _a: fn() -> ::a::A) {}
30+
}
31+
}
32+
33+
pub use self::b::new;
34+
35+
fn main() {}

0 commit comments

Comments
 (0)