Skip to content

librustc_typeck: use diag item instead of string compare #74471

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 2 commits into from
Jul 28, 2020
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
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ symbols! {
stop_after_dataflow,
str,
str_alloc,
string_type,
stringify,
struct_field_attributes,
struct_inherit,
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
on the left and may require reallocation. This \
requires ownership of the string on the left";

let is_std_string = |ty| &format!("{:?}", ty) == "std::string::String";
let string_type = self.tcx.get_diagnostic_item(sym::string_type);
let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
Some(ty_def) => Some(ty_def.did) == string_type,
None => false,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also use map_or here though.

ty.ty_adt_def().map_or(false, |adt_def| Some(adt_def.did) == string_type)


match (&lhs_ty.kind, &rhs_ty.kind) {
(&Ref(_, l_ty, _), &Ref(_, r_ty, _)) // &str or &String + &str, &String or &&str
Expand Down