-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Use Option::filter instead of open-coding it #80637
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Outdated
Show resolved
Hide resolved
The job Click to see the possible cause of the failure (guessed by this bot)
|
The job Click to see the possible cause of the failure (guessed by this bot)
|
Turned the call to |
Not sure if this is better or worse, but the first two method calls made me reach for the let typeck_results = self.infcx.in_progress_typeck_results?;
let ty = typeck_results.borrow().node_type_opt(hir_id)?;
let ty = self.infcx.resolve_vars_if_possible(ty);
for inner in ty.walk() {
if inner == self.target {
return Some(ty);
}
if let GenericArgKind::Type(inner_ty) = inner.unpack() {
if let ty::Infer(ty::TyVar(a_vid)) = inner_ty.kind() {
if let GenericArgKind::Type(target_ty) = self.target.unpack() {
if let ty::Infer(ty::TyVar(b_vid)) = target_ty.kind() {
if self.infcx.inner.borrow_mut().type_variables().sub_unified(a_vid, b_vid) {
return Some(ty);
}
}
}
}
}
}
None a bit much nesting, but there are no helper methods returning options that could be used here. Maybe we need to add them? Anyway, the diff lgtm as it is, so let me know if you want to merge it or iterate some more. r? @oli-obk |
This looks like a good candidate for |
Personally I'm a fan of method chaining and would like to keep that intact. Those Some helper to do fn node_ty_contains_target(&mut self, hir_id: HirId) -> Option<Ty<'tcx>> {
self.infcx
.in_progress_typeck_results
.and_then(|typeck_results| typeck_results.borrow().node_type_opt(hir_id))
.map(|ty| self.infcx.resolve_vars_if_possible(ty))
.filter(|ty| {
ty.walk().any(|inner| {
if inner == self.target {
return true;
}
let avid = Some(inner)
.if_let(GenericArgKind::Type(inner_ty), |inner| inner.unpack())
.if_let(ty::Infer(ty::TyVar(a_vid)), |inner_ty| inner_ty.kind());
let bvid = Some(self.target)
.if_let(GenericArgKind::Type(target_ty), |target| target.unpack())
.if_let(ty::Infer(ty::TyVar(b_vid)), |target_ty| target_ty.kind());
avid.zip(bvid).map_or(false, |(avid, bvid)| {
self.infcx.inner.borrow_mut().type_variables().sub_unified(a_vid, b_vid)
})
})
})
} For now I'd like to merge this as is, but it's certainly a spot for a followup if/when a fitting helper becomes available. |
@bors r+ rollup |
📌 Commit 203d502 has been approved by |
Rollup of 12 pull requests Successful merges: - rust-lang#80442 (Mention Arc::make_mut and Rc::make_mut in the documentation of Cow) - rust-lang#80533 (bootstrap: clippy fixes) - rust-lang#80538 (Add check for `[T;N]`/`usize` mismatch in astconv) - rust-lang#80612 (Remove reverted change from relnotes) - rust-lang#80627 (Builder: Warn if test file does not exist) - rust-lang#80637 (Use Option::filter instead of open-coding it) - rust-lang#80643 (Move variable into the only branch where it is relevant) - rust-lang#80656 (Fixed documentation error for `std::hint::spin_loop`) - rust-lang#80666 (Fix missing link for "fully qualified syntax") - rust-lang#80672 (./x.py clippy: allow the most noisy lints) - rust-lang#80677 (doc -- list edit for consistency) - rust-lang#80696 (make sure that promoteds which fail to evaluate in dead const code behave correctly) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
@rustbot modify labels +C-cleanup +T-compiler