use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
use rustc_session::lint::LintBuffer;
use rustc_session::Session;
-use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use std::mem;
}
}
- fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
- if asyncness.is_async() {
- struct_span_err!(self.session, span, E0706, "trait fns cannot be declared `async`")
- .note("`async` trait functions are not currently supported")
- .note(
- "consider using the `async-trait` crate: \
- https://crates.io/crates/async-trait",
- )
- .emit();
+ fn check_trait_fn_not_async(&self, fn_span: Span, asyncness: Async) {
+ if let Async::Yes { span, .. } = asyncness {
+ struct_span_err!(
+ self.session,
+ fn_span,
+ E0706,
+ "functions in traits cannot be declared `async`"
+ )
+ .span_label(span, "`async` because of this")
+ .note("`async` trait functions are not currently supported")
+ .note("consider using the `async-trait` crate: https://crates.io/crates/async-trait")
+ .emit();
}
}
- fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
- if constness.node == Constness::Const {
+ fn check_trait_fn_not_const(&self, constness: Const) {
+ if let Const::Yes(span) = constness {
struct_span_err!(
self.session,
- constness.span,
+ span,
E0379,
- "trait fns cannot be declared const"
+ "functions in traits cannot be declared const"
)
- .span_label(constness.span, "trait fns cannot be const")
+ .span_label(span, "functions in traits cannot be const")
.emit();
}
}
(Some(FnCtxt::Foreign), _) => return,
(Some(FnCtxt::Free), Some(header)) => match header.ext {
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }) | Extern::Implicit
- if header.unsafety == Unsafety::Unsafe =>
+ if matches!(header.unsafety, Unsafe::Yes(_)) =>
{
return;
}
/// FIXME(const_generics): Is this really true / necessary? Discuss with @varkor.
/// At any rate, the restriction feels too syntactic. Consider moving it to e.g. typeck.
fn check_const_fn_const_generic(&self, span: Span, sig: &FnSig, generics: &Generics) {
- if sig.header.constness.node == Constness::Const {
+ if let Const::Yes(const_span) = sig.header.constness {
// Look for const generics and error if we find any.
for param in &generics.params {
if let GenericParamKind::Const { .. } = param.kind {
self.err_handler()
- .struct_span_err(span, "const parameters are not permitted in `const fn`")
+ .struct_span_err(
+ span,
+ "const parameters are not permitted in const functions",
+ )
+ .span_label(const_span, "`const` because of this")
.emit();
}
}
.help("use `auto trait Trait {}` instead")
.emit();
}
- if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
+ if let (Unsafe::Yes(span), ImplPolarity::Negative) = (unsafety, polarity) {
struct_span_err!(
this.session,
item.span,
E0198,
"negative impls cannot be unsafe"
)
+ .span_label(span, "unsafe because of this")
.emit();
}
&item.vis,
Some("place qualifiers on individual impl items instead"),
);
- if unsafety == Unsafety::Unsafe {
+ if let Unsafe::Yes(span) = unsafety {
struct_span_err!(
self.session,
item.span,
E0197,
"inherent impls cannot be unsafe"
)
+ .span_label(span, "unsafe because of this")
.emit();
}
if polarity == ImplPolarity::Negative {
.note("only trait implementations may be annotated with default")
.emit();
}
- if constness == Constness::Const {
+ if let Const::Yes(span) = constness {
self.err_handler()
.struct_span_err(item.span, "inherent impls cannot be `const`")
+ .span_label(span, "`const` because of this")
.note("only trait implementations may be annotated with `const`")
.emit();
}
self.check_c_varadic_type(fk);
+ // Functions cannot both be `const async`
+ if let Some(FnHeader {
+ constness: Const::Yes(cspan),
+ asyncness: Async::Yes { span: aspan, .. },
+ ..
+ }) = fk.header()
+ {
+ self.err_handler()
+ .struct_span_err(span, "functions cannot be both `const` and `async`")
+ .span_label(*cspan, "`const` because of this")
+ .span_label(*aspan, "`async` because of this")
+ .emit();
+ }
+
// Functions without bodies cannot have patterns.
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
self.invalid_visibility(&item.vis, None);
if let AssocItemKind::Fn(sig, _) = &item.kind {
self.check_trait_fn_not_const(sig.header.constness);
- self.check_trait_fn_not_async(item.span, sig.header.asyncness.node);
+ self.check_trait_fn_not_async(item.span, sig.header.asyncness);
}
}