Skip to content

Treat safe target_feature functions as unsafe by default [less invasive variant] #134353

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 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Render fn defs with target_features attrs with the attribute [second …
…site]
  • Loading branch information
oli-obk committed Jan 15, 2025
commit 33651f49a0999c07f7f33bfd5d6d98bf91390837
52 changes: 35 additions & 17 deletions compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +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>])>,
fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
sig2: &ty::PolyFnSig<'tcx>,
fn_def2: Option<(DefId, &'tcx [ty::GenericArg<'tcx>])>,
fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
) -> (DiagStyledString, DiagStyledString) {
let sig1 = &(self.normalize_fn_sig)(*sig1);
let sig2 = &(self.normalize_fn_sig)(*sig2);
Expand Down Expand Up @@ -944,23 +944,23 @@ 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));
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);
(Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
let path1 = fmt(fn_def1, fn_args1);
let path2 = fmt(fn_def2, fn_args2);
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));
(Some((fn_def1, Some(fn_args1))), None) => {
values.0.push_highlighted(fmt(fn_def1, fn_args1));
}
(None, Some(fn_def2)) => {
values.1.push_highlighted(fmt(fn_def2));
(None, Some((fn_def2, Some(fn_args2)))) => {
values.1.push_highlighted(fmt(fn_def2, fn_args2));
}
_ => {}
}

values
Expand Down Expand Up @@ -1351,17 +1351,22 @@ 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);
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig2, Some((*did2, args2)))
self.cmp_fn_sig(
&sig1,
Some((*did1, Some(args1))),
&sig2,
Some((*did2, Some(args2))),
)
}

(ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
self.cmp_fn_sig(&sig1, Some((*did1, args1)), &sig_tys2.with(*hdr2), None)
self.cmp_fn_sig(&sig1, Some((*did1, Some(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);
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, args2)))
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
}

(ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
Expand Down Expand Up @@ -1543,7 +1548,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(false, Mismatch::Fixed("existential projection"))
}
};
let Some(vals) = self.values_str(values) else {
let Some(vals) = self.values_str(values, cause) else {
// Derived error. Cancel the emitter.
// NOTE(eddyb) this was `.cancel()`, but `diag`
// is borrowed, so we can't fully defuse it.
Expand Down Expand Up @@ -1968,7 +1973,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
| ObligationCauseCode::BlockTailExpression(.., source)) = code
&& let hir::MatchSource::TryDesugar(_) = source
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values)
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values, &trace.cause)
{
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
found: found_ty.content(),
Expand Down Expand Up @@ -2097,6 +2102,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
fn values_str(
&self,
values: ValuePairs<'tcx>,
cause: &ObligationCause<'tcx>,
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
match values {
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
Expand All @@ -2121,7 +2127,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if exp_found.references_error() {
return None;
}
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, None, &exp_found.found, None);
let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
impl_item_def_id,
trait_item_def_id,
..
} = *cause.code()
{
(Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
} else {
(None, None)
};

let (exp, fnd) =
self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2);
Some((exp, fnd, None))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
span: trace.cause.span,
requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
expected_found: self.values_str(trace.values).map(|(e, f, _)| (e, f)),
expected_found: self.values_str(trace.values, &trace.cause).map(|(e, f, _)| (e, f)),
}
.add_to_diag(err),
infer::Reborrow(span) => {
Expand Down Expand Up @@ -946,8 +946,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

if let infer::Subtype(ref sup_trace) = sup_origin
&& let infer::Subtype(ref sub_trace) = sub_origin
&& let Some((sup_expected, sup_found, _)) = self.values_str(sup_trace.values)
&& let Some((sub_expected, sub_found, _)) = self.values_str(sub_trace.values)
&& let Some((sup_expected, sup_found, _)) =
self.values_str(sup_trace.values, &sup_trace.cause)
&& let Some((sub_expected, sub_found, _)) =
self.values_str(sub_trace.values, &sup_trace.cause)
&& sub_expected == sup_expected
&& sub_found == sup_found
{
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/rfcs/rfc-2396-target_feature-11/trait-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
error: `#[target_feature(..)]` cannot be applied to safe trait method
--> $DIR/trait-impl.rs:13:5
|
LL | #[target_feature(enable = "sse2")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
LL |
LL | fn foo(&self) {}
| ------------- not an `unsafe` function

error[E0053]: method `foo` has an incompatible type for trait
--> $DIR/trait-impl.rs:15:5
|
Expand All @@ -10,16 +19,7 @@ note: type in trait
LL | fn foo(&self);
| ^^^^^^^^^^^^^^
= note: expected signature `fn(&Bar)`
found signature `unsafe fn(&Bar)`

error: `#[target_feature(..)]` cannot be applied to safe trait method
--> $DIR/trait-impl.rs:13:5
|
LL | #[target_feature(enable = "sse2")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
LL |
LL | fn foo(&self) {}
| ------------- not an `unsafe` function
found signature `#[target_features] fn(&Bar)`

error: `#[target_feature(..)]` cannot be applied to safe trait method
--> $DIR/trait-impl.rs:23:5
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/target-feature/invalid-attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,6 @@ LL | impl Quux for u8 {}
LL | fn foo();
| --------- `foo` from trait

error[E0053]: method `foo` has an incompatible type for trait
--> $DIR/invalid-attribute.rs:108:5
|
LL | fn foo() {}
| ^^^^^^^^ expected safe fn, found unsafe fn
|
note: type in trait
--> $DIR/invalid-attribute.rs:99:5
|
LL | fn foo();
| ^^^^^^^^^
= note: expected signature `fn()`
found signature `unsafe fn()`

error[E0658]: `#[target_feature(..)]` can only be applied to `unsafe` functions
--> $DIR/invalid-attribute.rs:104:5
|
Expand All @@ -219,6 +205,20 @@ LL | fn foo() {}
= help: add `#![feature(target_feature_11)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0053]: method `foo` has an incompatible type for trait
--> $DIR/invalid-attribute.rs:108:5
|
LL | fn foo() {}
| ^^^^^^^^ expected safe fn, found unsafe fn
|
note: type in trait
--> $DIR/invalid-attribute.rs:99:5
|
LL | fn foo();
| ^^^^^^^^^
= note: expected signature `fn()`
found signature `#[target_features] fn()`

error: aborting due to 24 previous errors

Some errors have detailed explanations: E0046, E0053, E0658.
Expand Down