Skip to content

Commit d09f5a4

Browse files
celinvaltedinski
authored andcommitted
Fix panic due to wrong intrinsic arguments (rust-lang#533) (rust-lang#610)
During codegen of function calls, we were ignoring argument of Unit type. This was causing a failure due to missing arguments when an intrinsic or closure was being called with an '()' as an argument. This change modifies how we deal with Unit types during function definition and function call.
1 parent e5c5acd commit d09f5a4

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

compiler/rustc_codegen_rmc/src/codegen/statement.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,30 +271,24 @@ impl<'tcx> GotocCtx<'tcx> {
271271
// Therefore, we have to project out the corresponding fields when we detect
272272
// an invocation of a closure.
273273
//
274-
// Note: In some cases, the enviroment struct has type FnDef, so we skip it in
274+
// Note: In some cases, the environment struct has type FnDef, so we skip it in
275275
// ignore_var_ty. So the tuple is always the last arg, but it might be in the
276276
// first or the second position.
277+
// Note 2: For empty closures, the only argument needed is the environment struct.
277278
if fargs.len() > 0 {
278279
let tupe = fargs.remove(fargs.len() - 1);
279280
let tupled_args: Vec<Type> = match self.operand_ty(last_mir_arg.unwrap()).kind() {
280281
ty::Tuple(tupled_args) => {
281-
// The tuple needs to be added back for type checking even if empty
282-
if tupled_args.is_empty() {
283-
fargs.push(tupe);
284-
return;
285-
}
286282
tupled_args.iter().map(|s| self.codegen_ty(s.expect_ty())).collect()
287283
}
288284
_ => unreachable!("Argument to function with Abi::RustCall is not a tuple"),
289285
};
290286

291287
// Unwrap as needed
292288
for (i, t) in tupled_args.iter().enumerate() {
293-
if !t.is_unit() {
294-
// Access the tupled parameters through the `member` operation
295-
let index_param = tupe.clone().member(&i.to_string(), &self.symbol_table);
296-
fargs.push(index_param);
297-
}
289+
// Access the tupled parameters through the `member` operation
290+
let index_param = tupe.clone().member(&i.to_string(), &self.symbol_table);
291+
fargs.push(index_param);
298292
}
299293
}
300294
}

compiler/rustc_codegen_rmc/src/codegen/typ.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ impl<'tcx> GotocCtx<'tcx> {
12381238
/// Whether a variable of type ty should be ignored as a parameter to a function
12391239
pub fn ignore_var_ty(&self, ty: Ty<'tcx>) -> bool {
12401240
match ty.kind() {
1241-
ty::Tuple(substs) if substs.is_empty() => true,
12421241
ty::FnDef(_, _) => true,
12431242
_ => false,
12441243
}

src/test/rmc/Iterator/try_fold.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// cbmc-flags: --unwind 3
4+
5+
pub fn main() {
6+
let arr = [(1, 2), (2, 2)];
7+
let result = arr.iter().try_fold((), |acc, &i| Some(()));
8+
assert_ne!(result, None, "This should succeed");
9+
}

0 commit comments

Comments
 (0)