Skip to content

Commit 2069650

Browse files
committed
Render fn defs with target_features attrs with the attribute [second site]
1 parent 6ddcd57 commit 2069650

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

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

+35-17
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
824824
fn cmp_fn_sig(
825825
&self,
826826
sig1: &ty::PolyFnSig<'tcx>,
827-
fn_def1: Option<(DefId, &'tcx [ty::GenericArg<'tcx>])>,
827+
fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
828828
sig2: &ty::PolyFnSig<'tcx>,
829-
fn_def2: Option<(DefId, &'tcx [ty::GenericArg<'tcx>])>,
829+
fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
830830
) -> (DiagStyledString, DiagStyledString) {
831831
let sig1 = &(self.normalize_fn_sig)(*sig1);
832832
let sig2 = &(self.normalize_fn_sig)(*sig2);
@@ -944,23 +944,23 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
944944
(values.1).0.extend(x2.0);
945945
}
946946

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

949949
match (fn_def1, fn_def2) {
950-
(None, None) => {}
951-
(Some(fn_def1), Some(fn_def2)) => {
952-
let path1 = fmt(fn_def1);
953-
let path2 = fmt(fn_def2);
950+
(Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
951+
let path1 = fmt(fn_def1, fn_args1);
952+
let path2 = fmt(fn_def2, fn_args2);
954953
let same_path = path1 == path2;
955954
values.0.push(path1, !same_path);
956955
values.1.push(path2, !same_path);
957956
}
958-
(Some(fn_def1), None) => {
959-
values.0.push_highlighted(fmt(fn_def1));
957+
(Some((fn_def1, Some(fn_args1))), None) => {
958+
values.0.push_highlighted(fmt(fn_def1, fn_args1));
960959
}
961-
(None, Some(fn_def2)) => {
962-
values.1.push_highlighted(fmt(fn_def2));
960+
(None, Some((fn_def2, Some(fn_args2)))) => {
961+
values.1.push_highlighted(fmt(fn_def2, fn_args2));
963962
}
963+
_ => {}
964964
}
965965

966966
values
@@ -1351,17 +1351,22 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
13511351
(ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
13521352
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
13531353
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1354-
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig2, Some((*did2, args2)))
1354+
self.cmp_fn_sig(
1355+
&sig1,
1356+
Some((*did1, Some(args1))),
1357+
&sig2,
1358+
Some((*did2, Some(args2))),
1359+
)
13551360
}
13561361

13571362
(ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
13581363
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
1359-
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig_tys2.with(*hdr2), None)
1364+
self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
13601365
}
13611366

13621367
(ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
13631368
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1364-
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, args2)))
1369+
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
13651370
}
13661371

13671372
(ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
@@ -1543,7 +1548,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15431548
(false, Mismatch::Fixed("existential projection"))
15441549
}
15451550
};
1546-
let Some(vals) = self.values_str(values) else {
1551+
let Some(vals) = self.values_str(values, cause) else {
15471552
// Derived error. Cancel the emitter.
15481553
// NOTE(eddyb) this was `.cancel()`, but `diag`
15491554
// is borrowed, so we can't fully defuse it.
@@ -1968,7 +1973,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19681973
})
19691974
| ObligationCauseCode::BlockTailExpression(.., source)) = code
19701975
&& let hir::MatchSource::TryDesugar(_) = source
1971-
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values)
1976+
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values, &trace.cause)
19721977
{
19731978
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
19741979
found: found_ty.content(),
@@ -2097,6 +2102,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
20972102
fn values_str(
20982103
&self,
20992104
values: ValuePairs<'tcx>,
2105+
cause: &ObligationCause<'tcx>,
21002106
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
21012107
match values {
21022108
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
@@ -2121,7 +2127,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21212127
if exp_found.references_error() {
21222128
return None;
21232129
}
2124-
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, None, &exp_found.found, None);
2130+
let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2131+
impl_item_def_id,
2132+
trait_item_def_id,
2133+
..
2134+
} = *cause.code()
2135+
{
2136+
(Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
2137+
} else {
2138+
(None, None)
2139+
};
2140+
2141+
let (exp, fnd) =
2142+
self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2);
21252143
Some((exp, fnd, None))
21262144
}
21272145
}

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

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

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

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error: `#[target_feature(..)]` cannot be applied to safe trait method
2+
--> $DIR/trait-impl.rs:13:5
3+
|
4+
LL | #[target_feature(enable = "sse2")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
6+
LL |
7+
LL | fn foo(&self) {}
8+
| ------------- not an `unsafe` function
9+
110
error[E0053]: method `foo` has an incompatible type for trait
211
--> $DIR/trait-impl.rs:15:5
312
|
@@ -10,16 +19,7 @@ note: type in trait
1019
LL | fn foo(&self);
1120
| ^^^^^^^^^^^^^^
1221
= note: expected signature `fn(&Bar)`
13-
found signature `unsafe fn(&Bar)`
14-
15-
error: `#[target_feature(..)]` cannot be applied to safe trait method
16-
--> $DIR/trait-impl.rs:13:5
17-
|
18-
LL | #[target_feature(enable = "sse2")]
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
20-
LL |
21-
LL | fn foo(&self) {}
22-
| ------------- not an `unsafe` function
22+
found signature `#[target_features] fn(&Bar)`
2323

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

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,6 @@ LL | impl Quux for u8 {}
192192
LL | fn foo();
193193
| --------- `foo` from trait
194194

195-
error[E0053]: method `foo` has an incompatible type for trait
196-
--> $DIR/invalid-attribute.rs:108:5
197-
|
198-
LL | fn foo() {}
199-
| ^^^^^^^^ expected safe fn, found unsafe fn
200-
|
201-
note: type in trait
202-
--> $DIR/invalid-attribute.rs:99:5
203-
|
204-
LL | fn foo();
205-
| ^^^^^^^^^
206-
= note: expected signature `fn()`
207-
found signature `unsafe fn()`
208-
209195
error[E0658]: `#[target_feature(..)]` can only be applied to `unsafe` functions
210196
--> $DIR/invalid-attribute.rs:104:5
211197
|
@@ -219,6 +205,20 @@ LL | fn foo() {}
219205
= help: add `#![feature(target_feature_11)]` to the crate attributes to enable
220206
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
221207

208+
error[E0053]: method `foo` has an incompatible type for trait
209+
--> $DIR/invalid-attribute.rs:108:5
210+
|
211+
LL | fn foo() {}
212+
| ^^^^^^^^ expected safe fn, found unsafe fn
213+
|
214+
note: type in trait
215+
--> $DIR/invalid-attribute.rs:99:5
216+
|
217+
LL | fn foo();
218+
| ^^^^^^^^^
219+
= note: expected signature `fn()`
220+
found signature `#[target_features] fn()`
221+
222222
error: aborting due to 24 previous errors
223223

224224
Some errors have detailed explanations: E0046, E0053, E0658.

0 commit comments

Comments
 (0)