Skip to content

Commit

Permalink
Replace trait_is_async_fn with async_fn_trait_kind_from_def_id
Browse files Browse the repository at this point in the history
  • Loading branch information
cramertj committed Nov 21, 2024
1 parent 67d42b5 commit 0fc289a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.trait_is_alias(trait_def_id)
}

fn trait_is_async_fn(self, trait_def_id: DefId) -> bool {
self.trait_is_async_fn(trait_def_id)
}

fn trait_is_dyn_compatible(self, trait_def_id: DefId) -> bool {
self.is_dyn_compatible(trait_def_id)
}
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,17 +1830,6 @@ impl<'tcx> TyCtxt<'tcx> {
self.trait_def(trait_def_id).has_auto_impl
}

/// Returns `true` if this is an `AsyncFn`, `AsyncFnMut`, or `AsyncFnOnce` trait.
pub fn trait_is_async_fn(self, trait_def_id: DefId) -> bool {
let lang_items = self.lang_items();
[
lang_items.async_fn_trait(),
lang_items.async_fn_mut_trait(),
lang_items.async_fn_once_trait(),
]
.contains(&Some(trait_def_id))
}

/// Returns `true` if this is coinductive, either because it is
/// an auto trait or because it has the `#[rustc_coinductive]` attribute.
pub fn trait_is_coinductive(self, trait_def_id: DefId) -> bool {
Expand Down
28 changes: 12 additions & 16 deletions compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_infer::traits::{
};
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::{ErrorGuaranteed, ExpnKind, Span, Symbol};
use rustc_span::{ErrorGuaranteed, ExpnKind, Span};
use tracing::{info, instrument};

pub use self::overflow::*;
Expand Down Expand Up @@ -431,35 +431,31 @@ pub fn report_dyn_incompatibility<'tcx>(

// Avoid errors diving into the details of the `AsyncFn` traits if this is
// a straightforward "`AsyncFn` is not yet dyn compatible" error.
if tcx.trait_is_async_fn(trait_def_id) {
let fn_trait: Symbol = violations
.iter()
.find_map(|violation| {
// Try to find the original trait that caused the violation.
match *violation {
DynCompatibilityViolation::AsyncFnTrait { fn_trait } => Some(fn_trait),
_ => None,
}
})
.expect("AsyncFn traits should report a corresponding DynCompatibilityViolation");
if let Some(async_fn_trait_kind) = tcx.async_fn_trait_kind_from_def_id(trait_def_id) {
let async_fn_trait_name = match async_fn_trait_kind {
ty::ClosureKind::Fn => "AsyncFn",
ty::ClosureKind::FnMut => "AsyncFnMut",
ty::ClosureKind::FnOnce => "AsyncFnOnce",
};

let mut err = struct_span_code_err!(
tcx.dcx(),
span,
E0802,
"the trait `{}` is not yet dyn compatible",
fn_trait
async_fn_trait_name
);
// Note: this check is quite imprecise.
// Comparing the DefIds or similar would be better, but we can't store
// DefIds in `DynCompatibilityViolation`.
if fn_trait.as_str() == trait_str {
err.span_label(span, format!("`{fn_trait}` is not yet dyn compatible"));
if async_fn_trait_name == trait_str {
err.span_label(span, format!("`{async_fn_trait_name}` is not yet dyn compatible"));
} else {
let trait_str = tcx.def_path_str(trait_def_id);
err.span_label(
span,
format!(
"`{trait_str}` inherits from `{fn_trait}` which is not yet dyn compatible'"
"`{trait_str}` inherits from `{async_fn_trait_name}` which is not yet dyn compatible'"
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn hir_ty_lowering_dyn_compatibility_violations(
// Check for `AsyncFn` traits first to avoid reporting various other
// errors about the details of why these traits aren't object-safe.
for supertrait in tcx.supertrait_def_ids(trait_def_id) {
if tcx.trait_is_async_fn(supertrait) {
if tcx.async_fn_trait_kind_from_def_id(supertrait).is_some() {
let fn_trait = tcx.item_name(supertrait);
return vec![DynCompatibilityViolation::AsyncFnTrait { fn_trait }];
}
Expand All @@ -66,7 +66,7 @@ fn dyn_compatibility_violations(
// Check for `AsyncFn` traits first to avoid reporting various other
// errors about the details of why these traits aren't object-safe.
for supertrait in tcx.supertrait_def_ids(trait_def_id) {
if tcx.trait_is_async_fn(supertrait) {
if tcx.async_fn_trait_kind_from_def_id(supertrait).is_some() {
let fn_trait = tcx.item_name(supertrait);
return core::slice::from_ref(
tcx.arena.alloc(DynCompatibilityViolation::AsyncFnTrait { fn_trait }),
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ pub trait Interner:

fn trait_is_alias(self, trait_def_id: Self::DefId) -> bool;

fn trait_is_async_fn(self, trait_def_id: Self::DefId) -> bool;

fn trait_is_dyn_compatible(self, trait_def_id: Self::DefId) -> bool;

fn trait_is_fundamental(self, def_id: Self::DefId) -> bool;
Expand Down

0 comments on commit 0fc289a

Please sign in to comment.