Skip to content

Commit

Permalink
Handle fndef rendering together with signature rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 16, 2024
1 parent d185062 commit e40a494
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
46 changes: 26 additions & 20 deletions compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
fn cmp_fn_sig(
&self,
sig1: &ty::PolyFnSig<'tcx>,
fn_def1: Option<(DefId, &'tcx [ty::GenericArg<'tcx>])>,
sig2: &ty::PolyFnSig<'tcx>,
fn_def2: Option<(DefId, &'tcx [ty::GenericArg<'tcx>])>,
) -> (DiagStyledString, DiagStyledString) {
let sig1 = &(self.normalize_fn_sig)(*sig1);
let sig2 = &(self.normalize_fn_sig)(*sig2);
Expand Down Expand Up @@ -930,6 +932,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(values.1).0.extend(x2.0);
}

let fmt = |(did, args)| format!(" {{{}}}", self.tcx.def_path_str_with_args(did, args));

match (fn_def1, fn_def2) {
(None, None) => {}
(Some(fn_def1), Some(fn_def2)) => {
let path1 = fmt(fn_def1);
let path2 = fmt(fn_def2);
let same_path = path1 == path2;
values.0.push(path1, !same_path);
values.1.push(path2, !same_path);
}
(Some(fn_def1), None) => {
values.0.push_highlighted(fmt(fn_def1));
}
(None, Some(fn_def2)) => {
values.1.push_highlighted(fmt(fn_def2));
}
}

values
}

Expand Down Expand Up @@ -1318,36 +1339,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
let mut values = self.cmp_fn_sig(&sig1, &sig2);
let path1 = format!(" {{{}}}", self.tcx.def_path_str_with_args(*did1, args1));
let path2 = format!(" {{{}}}", self.tcx.def_path_str_with_args(*did2, args2));
let same_path = path1 == path2;
values.0.push(path1, !same_path);
values.1.push(path2, !same_path);
values
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig2, Some((*did2, args2)))
}

(ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
let mut values = self.cmp_fn_sig(&sig1, &sig_tys2.with(*hdr2));
values.0.push_highlighted(format!(
" {{{}}}",
self.tcx.def_path_str_with_args(*did1, args1)
));
values
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig_tys2.with(*hdr2), None)
}

(ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
let mut values = self.cmp_fn_sig(&sig_tys1.with(*hdr1), &sig2);
values
.1
.push_normal(format!(" {{{}}}", self.tcx.def_path_str_with_args(*did2, args2)));
values
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, args2)))
}

(ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
self.cmp_fn_sig(&sig_tys1.with(*hdr1), &sig_tys2.with(*hdr2))
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
}

_ => {
Expand Down Expand Up @@ -2102,7 +2108,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if exp_found.references_error() {
return None;
}
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found);
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, None, &exp_found.found, None);
Some((exp, fnd, None))
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-emitter/highlighting.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/ui/error-emitter/highlighting.windows.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/ui/error-emitter/unicode-output.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e40a494

Please sign in to comment.