Skip to content

Commit 34b2aa9

Browse files
committed
Render fn defs with target_features attrs with the attribute [second site]
1 parent bff21ee commit 34b2aa9

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15431543
(false, Mismatch::Fixed("existential projection"))
15441544
}
15451545
};
1546-
let Some(vals) = self.values_str(values) else {
1546+
let Some(vals) = self.values_str(values, cause) else {
15471547
// Derived error. Cancel the emitter.
15481548
// NOTE(eddyb) this was `.cancel()`, but `diag`
15491549
// is borrowed, so we can't fully defuse it.
@@ -1969,7 +1969,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19691969
})
19701970
| ObligationCauseCode::BlockTailExpression(.., source)) = code
19711971
&& let hir::MatchSource::TryDesugar(_) = source
1972-
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values)
1972+
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values, &trace.cause)
19731973
{
19741974
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
19751975
found: found_ty.content(),
@@ -2096,6 +2096,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
20962096
fn values_str(
20972097
&self,
20982098
values: ValuePairs<'tcx>,
2099+
cause: &ObligationCause<'tcx>,
20992100
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
21002101
match values {
21012102
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
@@ -2120,7 +2121,28 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21202121
if exp_found.references_error() {
21212122
return None;
21222123
}
2123-
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, None, &exp_found.found, None);
2124+
let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2125+
impl_item_def_id,
2126+
trait_item_def_id,
2127+
..
2128+
} = *cause.code()
2129+
{
2130+
(
2131+
Some((
2132+
trait_item_def_id,
2133+
&ty::GenericArgs::identity_for_item(self.tcx, trait_item_def_id)[..],
2134+
)),
2135+
Some((
2136+
impl_item_def_id.to_def_id(),
2137+
&ty::GenericArgs::identity_for_item(self.tcx, impl_item_def_id)[..],
2138+
)),
2139+
)
2140+
} else {
2141+
(None, None)
2142+
};
2143+
2144+
let (exp, fnd) =
2145+
self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2);
21242146
Some((exp, fnd, None))
21252147
}
21262148
}

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use rustc_errors::ErrorGuaranteed;
44
use rustc_hir as hir;
55
use rustc_hir::def::{Namespace, Res};
6-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::{DefId, LocalDefId};
77
use rustc_hir::intravisit::Visitor;
88
use rustc_middle::hir::nested_filter;
99
use rustc_middle::traits::ObligationCauseCode;
1010
use rustc_middle::ty::error::ExpectedFound;
1111
use rustc_middle::ty::print::RegionHighlightMode;
1212
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
13-
use rustc_span::Span;
13+
use rustc_span::{Span, sym};
1414
use tracing::debug;
1515

1616
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
@@ -33,14 +33,21 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3333
_,
3434
) = error.clone()
3535
&& let (Subtype(sup_trace), Subtype(sub_trace)) = (&sup_origin, &sub_origin)
36-
&& let &ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } =
37-
sub_trace.cause.code()
36+
&& let &ObligationCauseCode::CompareImplItem {
37+
trait_item_def_id, impl_item_def_id, ..
38+
} = sub_trace.cause.code()
3839
&& sub_trace.values == sup_trace.values
3940
&& let ValuePairs::PolySigs(ExpectedFound { expected, found }) = sub_trace.values
4041
{
4142
// FIXME(compiler-errors): Don't like that this needs `Ty`s, but
4243
// all of the region highlighting machinery only deals with those.
43-
let guar = self.emit_err(var_origin.span(), expected, found, trait_item_def_id);
44+
let guar = self.emit_err(
45+
var_origin.span(),
46+
expected,
47+
found,
48+
trait_item_def_id,
49+
impl_item_def_id,
50+
);
4451
return Some(guar);
4552
}
4653
None
@@ -52,6 +59,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5259
expected: ty::PolyFnSig<'tcx>,
5360
found: ty::PolyFnSig<'tcx>,
5461
trait_item_def_id: DefId,
62+
impl_item_def_id: LocalDefId,
5563
) -> ErrorGuaranteed {
5664
let trait_sp = self.tcx().def_span(trait_item_def_id);
5765

@@ -82,18 +90,31 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
8290

8391
let expected_highlight = HighlightBuilder::build(expected);
8492
let tcx = self.cx.tcx;
85-
let expected = Highlighted {
93+
let mut expected = Highlighted {
8694
highlight: expected_highlight,
8795
ns: Namespace::TypeNS,
8896
tcx,
8997
value: expected,
9098
}
9199
.to_string();
100+
// Cannot look at parsed codegen attributes, as those don't get emitted while we don't
101+
// allow target_features on trait methods.
102+
if let Some(trait_item_def_id) = trait_item_def_id.as_local() {
103+
if tcx.has_attr(trait_item_def_id, sym::target_feature) {
104+
expected = format!("#[target_features] {expected}");
105+
}
106+
}
92107
let found_highlight = HighlightBuilder::build(found);
93-
let found =
108+
let mut found =
94109
Highlighted { highlight: found_highlight, ns: Namespace::TypeNS, tcx, value: found }
95110
.to_string();
96111

112+
// Cannot look at parsed codegen attributes, as those don't get emitted while we don't
113+
// allow target_features on trait impl methods.
114+
if tcx.has_attr(impl_item_def_id, sym::target_feature) {
115+
found = format!("#[target_features] {found}");
116+
}
117+
97118
// Get the span of all the used type parameters in the method.
98119
let assoc_item = self.tcx().associated_item(trait_item_def_id);
99120
let mut visitor = TypeParamSpanVisitor { tcx: self.tcx(), types: vec![] };

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
222222
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
223223
span: trace.cause.span,
224224
requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
225-
expected_found: self.values_str(trace.values).map(|(e, f, _)| (e, f)),
225+
expected_found: self.values_str(trace.values, &trace.cause).map(|(e, f, _)| (e, f)),
226226
}
227227
.add_to_diag(err),
228228
infer::Reborrow(span) => {
@@ -947,8 +947,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
947947

948948
if let infer::Subtype(ref sup_trace) = sup_origin
949949
&& let infer::Subtype(ref sub_trace) = sub_origin
950-
&& let Some((sup_expected, sup_found, _)) = self.values_str(sup_trace.values)
951-
&& let Some((sub_expected, sub_found, _)) = self.values_str(sub_trace.values)
950+
&& let Some((sup_expected, sup_found, _)) =
951+
self.values_str(sup_trace.values, &sup_trace.cause)
952+
&& let Some((sub_expected, sub_found, _)) =
953+
self.values_str(sub_trace.values, &sup_trace.cause)
952954
&& sub_expected == sup_expected
953955
&& sub_found == sup_found
954956
{

tests/ui/rfcs/rfc-2396-target_feature-11/trait-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: type in trait
1010
LL | fn foo(&self);
1111
| ^^^^^^^^^^^^^^
1212
= note: expected signature `fn(&Bar)`
13-
found signature `unsafe fn(&Bar)`
13+
found signature `#[target_feature] unsafe fn(&Bar)`
1414

1515
error: `#[target_feature(..)]` cannot be applied to safe trait method
1616
--> $DIR/trait-impl.rs:13:5

tests/ui/target-feature/invalid-attribute.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ note: type in trait
204204
LL | fn foo();
205205
| ^^^^^^^^^
206206
= note: expected signature `fn()`
207-
found signature `unsafe fn()`
207+
found signature `#[target_feature] unsafe fn()`
208208

209209
error[E0658]: `#[target_feature(..)]` can only be applied to `unsafe` functions
210210
--> $DIR/invalid-attribute.rs:104:5

0 commit comments

Comments
 (0)