-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Deduplicate operand creation between scalars, non-scalars and string patterns #136121
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,66 +141,71 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
self.cfg.terminate(block, self.source_info(match_start_span), terminator); | ||
} | ||
|
||
TestKind::Eq { value, ty } => { | ||
TestKind::Eq { value, mut ty } => { | ||
let tcx = self.tcx; | ||
let success_block = target_block(TestBranch::Success); | ||
let fail_block = target_block(TestBranch::Failure); | ||
if let ty::Adt(def, _) = ty.kind() | ||
&& tcx.is_lang_item(def.did(), LangItem::String) | ||
{ | ||
if !tcx.features().string_deref_patterns() { | ||
span_bug!( | ||
|
||
let expect_ty = value.ty(); | ||
let expect = self.literal_operand(test.span, value); | ||
|
||
let mut place = place; | ||
let mut block = block; | ||
match ty.kind() { | ||
ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::String) => { | ||
if !tcx.features().string_deref_patterns() { | ||
span_bug!( | ||
test.span, | ||
"matching on `String` went through without enabling string_deref_patterns" | ||
); | ||
} | ||
let re_erased = tcx.lifetimes.re_erased; | ||
let ref_str_ty = Ty::new_imm_ref(tcx, re_erased, tcx.types.str_); | ||
let ref_str = self.temp(ref_str_ty, test.span); | ||
let eq_block = self.cfg.start_new_block(); | ||
// `let ref_str: &str = <String as Deref>::deref(&place);` | ||
self.call_deref( | ||
block, | ||
eq_block, | ||
place, | ||
Mutability::Not, | ||
ty, | ||
ref_str, | ||
test.span, | ||
"matching on `String` went through without enabling string_deref_patterns" | ||
); | ||
// Since we generated a `ref_str = <String as Deref>::deref(&place) -> eq_block` terminator, | ||
// we need to add all further statements to `eq_block`. | ||
// Similarly, the normal test code should be generated for the `&str`, instead of the `String`. | ||
block = eq_block; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a comment about why we update these. i.e. something like we're lowering to X. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
place = ref_str; | ||
ty = ref_str_ty; | ||
} | ||
let re_erased = tcx.lifetimes.re_erased; | ||
let ref_str_ty = Ty::new_imm_ref(tcx, re_erased, tcx.types.str_); | ||
let ref_str = self.temp(ref_str_ty, test.span); | ||
let eq_block = self.cfg.start_new_block(); | ||
// `let ref_str: &str = <String as Deref>::deref(&place);` | ||
self.call_deref( | ||
block, | ||
eq_block, | ||
place, | ||
Mutability::Not, | ||
ty, | ||
ref_str, | ||
test.span, | ||
); | ||
self.non_scalar_compare( | ||
eq_block, | ||
success_block, | ||
fail_block, | ||
source_info, | ||
value, | ||
ref_str, | ||
ref_str_ty, | ||
); | ||
} else if !ty.is_scalar() { | ||
_ => {} | ||
} | ||
|
||
if !ty.is_scalar() { | ||
// Use `PartialEq::eq` instead of `BinOp::Eq` | ||
// (the binop can only handle primitives) | ||
self.non_scalar_compare( | ||
block, | ||
success_block, | ||
fail_block, | ||
source_info, | ||
value, | ||
place, | ||
expect, | ||
expect_ty, | ||
Operand::Copy(place), | ||
ty, | ||
); | ||
} else { | ||
assert_eq!(value.ty(), ty); | ||
let expect = self.literal_operand(test.span, value); | ||
let val = Operand::Copy(place); | ||
assert_eq!(expect_ty, ty); | ||
self.compare( | ||
block, | ||
success_block, | ||
fail_block, | ||
source_info, | ||
BinOp::Eq, | ||
expect, | ||
val, | ||
Operand::Copy(place), | ||
); | ||
} | ||
} | ||
|
@@ -371,12 +376,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
success_block: BasicBlock, | ||
fail_block: BasicBlock, | ||
source_info: SourceInfo, | ||
value: Const<'tcx>, | ||
mut val: Place<'tcx>, | ||
mut expect: Operand<'tcx>, | ||
expect_ty: Ty<'tcx>, | ||
mut val: Operand<'tcx>, | ||
mut ty: Ty<'tcx>, | ||
) { | ||
let mut expect = self.literal_operand(source_info.span, value); | ||
|
||
// If we're using `b"..."` as a pattern, we need to insert an | ||
// unsizing coercion, as the byte string has the type `&[u8; N]`. | ||
// | ||
|
@@ -391,7 +395,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
_ => None, | ||
}; | ||
let opt_ref_ty = unsize(ty); | ||
let opt_ref_test_ty = unsize(value.ty()); | ||
let opt_ref_test_ty = unsize(expect_ty); | ||
match (opt_ref_ty, opt_ref_test_ty) { | ||
// nothing to do, neither is an array | ||
(None, None) => {} | ||
|
@@ -410,11 +414,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
PointerCoercion::Unsize, | ||
CoercionSource::Implicit, | ||
), | ||
Operand::Copy(val), | ||
val, | ||
ty, | ||
), | ||
); | ||
val = temp; | ||
val = Operand::Copy(temp); | ||
} | ||
if opt_ref_test_ty.is_some() { | ||
let slice = self.temp(ty, source_info.span); | ||
|
@@ -470,11 +474,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |
|
||
const_: method, | ||
})), | ||
args: [Spanned { node: Operand::Copy(val), span: DUMMY_SP }, Spanned { | ||
node: expect, | ||
span: DUMMY_SP, | ||
}] | ||
.into(), | ||
args: [Spanned { node: val, span: DUMMY_SP }, Spanned { node: expect, span: DUMMY_SP }] | ||
.into(), | ||
destination: eq_result, | ||
target: Some(eq_block), | ||
unwind: UnwindAction::Continue, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any stylistic reason for the
match
with a single arm over the let chain?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that I'll be adding a new variant immediately in a follow-up PR 😆