Refactor away the need for some descr
methods.
Instead we use `Display` impls and their `alternate` render scheme to decide whether we want backticks or not.
This commit is contained in:
parent
92b41eeee6
commit
c601ade3ad
@ -372,7 +372,7 @@ fn build_error(
|
||||
ccx: &ConstCx<'_, 'tcx>,
|
||||
span: Span,
|
||||
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
||||
let msg = format!("{}s are not allowed in {}s", self.0.descr(), ccx.const_kind());
|
||||
let msg = format!("{:#}s are not allowed in {}s", self.0, ccx.const_kind());
|
||||
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
|
||||
ccx.tcx.sess.create_feature_err(
|
||||
errors::UnallowedOpInConstContext { span, msg },
|
||||
|
@ -1520,21 +1520,19 @@ pub enum CoroutineKind {
|
||||
impl fmt::Display for CoroutineKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
CoroutineKind::Async(k) => fmt::Display::fmt(k, f),
|
||||
CoroutineKind::Async(k) => {
|
||||
if f.alternate() {
|
||||
f.write_str("`async` ")?;
|
||||
} else {
|
||||
f.write_str("async ")?
|
||||
}
|
||||
k.fmt(f)
|
||||
}
|
||||
CoroutineKind::Coroutine => f.write_str("coroutine"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CoroutineKind {
|
||||
pub fn descr(&self) -> &'static str {
|
||||
match self {
|
||||
CoroutineKind::Async(ask) => ask.descr(),
|
||||
CoroutineKind::Coroutine => "coroutine",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In the case of a coroutine created as part of an async/gen construct,
|
||||
/// which kind of async/gen construct caused it to be created?
|
||||
///
|
||||
@ -1555,21 +1553,12 @@ pub enum CoroutineSource {
|
||||
|
||||
impl fmt::Display for CoroutineSource {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match self {
|
||||
CoroutineSource::Block => "async block",
|
||||
CoroutineSource::Closure => "async closure body",
|
||||
CoroutineSource::Fn => "async fn body",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl CoroutineSource {
|
||||
pub fn descr(&self) -> &'static str {
|
||||
match self {
|
||||
CoroutineSource::Block => "`async` block",
|
||||
CoroutineSource::Closure => "`async` closure body",
|
||||
CoroutineSource::Fn => "`async` fn body",
|
||||
CoroutineSource::Block => "block",
|
||||
CoroutineSource::Closure => "closure body",
|
||||
CoroutineSource::Fn => "fn body",
|
||||
}
|
||||
.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1584,14 +1584,13 @@ fn add_labels_for_types(
|
||||
target: &str,
|
||||
types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
|
||||
) {
|
||||
for (key, values) in types.iter() {
|
||||
for (kind, values) in types.iter() {
|
||||
let count = values.len();
|
||||
let kind = key.descr();
|
||||
for &sp in values {
|
||||
err.span_label(
|
||||
sp,
|
||||
format!(
|
||||
"{}{} {}{}",
|
||||
"{}{} {:#}{}",
|
||||
if count == 1 { "the " } else { "one of the " },
|
||||
target,
|
||||
kind,
|
||||
@ -2952,17 +2951,19 @@ pub enum TyCategory {
|
||||
Foreign,
|
||||
}
|
||||
|
||||
impl TyCategory {
|
||||
fn descr(&self) -> &'static str {
|
||||
impl fmt::Display for TyCategory {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Closure => "closure",
|
||||
Self::Opaque => "opaque type",
|
||||
Self::OpaqueFuture => "future",
|
||||
Self::Coroutine(gk) => gk.descr(),
|
||||
Self::Foreign => "foreign type",
|
||||
Self::Closure => "closure".fmt(f),
|
||||
Self::Opaque => "opaque type".fmt(f),
|
||||
Self::OpaqueFuture => "future".fmt(f),
|
||||
Self::Coroutine(gk) => gk.fmt(f),
|
||||
Self::Foreign => "foreign type".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TyCategory {
|
||||
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
|
||||
match *ty.kind() {
|
||||
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
|
||||
|
@ -241,7 +241,9 @@ pub fn sort_string(self, tcx: TyCtxt<'tcx>) -> Cow<'static, str> {
|
||||
}
|
||||
ty::Dynamic(..) => "trait object".into(),
|
||||
ty::Closure(..) => "closure".into(),
|
||||
ty::Coroutine(def_id, ..) => tcx.coroutine_kind(def_id).unwrap().descr().into(),
|
||||
ty::Coroutine(def_id, ..) => {
|
||||
format!("{:#}", tcx.coroutine_kind(def_id).unwrap()).into()
|
||||
}
|
||||
ty::CoroutineWitness(..) => "coroutine witness".into(),
|
||||
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
|
||||
ty::Infer(ty::IntVar(_)) => "integer".into(),
|
||||
@ -299,7 +301,9 @@ pub fn prefix_string(self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
|
||||
ty::FnPtr(_) => "fn pointer".into(),
|
||||
ty::Dynamic(..) => "trait object".into(),
|
||||
ty::Closure(..) => "closure".into(),
|
||||
ty::Coroutine(def_id, ..) => tcx.coroutine_kind(def_id).unwrap().descr().into(),
|
||||
ty::Coroutine(def_id, ..) => {
|
||||
format!("{:#}", tcx.coroutine_kind(def_id).unwrap()).into()
|
||||
}
|
||||
ty::CoroutineWitness(..) => "coroutine witness".into(),
|
||||
ty::Tuple(..) => "tuple".into(),
|
||||
ty::Placeholder(..) => "higher-ranked type".into(),
|
||||
|
@ -2995,11 +2995,11 @@ fn note_obligation_cause_code<T>(
|
||||
let sp = self.tcx.def_span(def_id);
|
||||
|
||||
// Special-case this to say "async block" instead of `[static coroutine]`.
|
||||
let kind = tcx.coroutine_kind(def_id).unwrap().descr();
|
||||
let kind = tcx.coroutine_kind(def_id).unwrap();
|
||||
err.span_note(
|
||||
sp,
|
||||
with_forced_trimmed_paths!(format!(
|
||||
"required because it's used within this {kind}",
|
||||
"required because it's used within this {kind:#}",
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user