Skip to content

Fix misoptimizations when matching against strings/slices #22385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/librustc_trans/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,19 @@ fn compare_values<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
&format!("comparison of `{}`",
cx.ty_to_string(rhs_t))[],
StrEqFnLangItem);
callee::trans_lang_call(cx, did, &[lhs, rhs], None, debug_loc)
let t = ty::mk_str_slice(cx.tcx(), cx.tcx().mk_region(ty::ReStatic), ast::MutImmutable);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about why this is doing seemingly pointless copies?

// The comparison function gets the slices by value, so we have to make copies here. Even
// if the function doesn't write through the pointer, things like lifetime intrinsics
// require that we do this properly
let lhs_arg = alloc_ty(cx, t, "lhs");
let rhs_arg = alloc_ty(cx, t, "rhs");
memcpy_ty(cx, lhs_arg, lhs, t);
memcpy_ty(cx, rhs_arg, rhs, t);
let res = callee::trans_lang_call(cx, did, &[lhs_arg, rhs_arg], None, debug_loc);
call_lifetime_end(res.bcx, lhs_arg);
call_lifetime_end(res.bcx, rhs_arg);

res
}

let _icx = push_ctxt("compare_values");
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/issue22008.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2015 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably have a // compile-flags: -O to force optimizations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this break when the tests are compiled with optimizations, because -O is given twice then. So I'll remove that again.

pub fn main() {
let command = "a";

match command {
"foo" => println!("foo"),
_ => println!("{}", command),
}
}