Silence unused type param and inference errors on struct parse error
This commit is contained in:
parent
2f1bd3f378
commit
9750d22c17
6 changed files with 96 additions and 2 deletions
|
|
@ -4525,6 +4525,26 @@ impl ItemKind<'_> {
|
|||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn recovered(&self) -> bool {
|
||||
match self {
|
||||
ItemKind::Struct(
|
||||
_,
|
||||
_,
|
||||
VariantData::Struct { recovered: ast::Recovered::Yes(_), .. },
|
||||
) => true,
|
||||
ItemKind::Union(
|
||||
_,
|
||||
_,
|
||||
VariantData::Struct { recovered: ast::Recovered::Yes(_), .. },
|
||||
) => true,
|
||||
ItemKind::Enum(_, _, def) => def.variants.iter().any(|v| match v.data {
|
||||
VariantData::Struct { recovered: ast::Recovered::Yes(_), .. } => true,
|
||||
_ => false,
|
||||
}),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The bodies for items are stored "out of line", in a separate
|
||||
|
|
|
|||
|
|
@ -2160,7 +2160,12 @@ fn report_bivariance<'tcx>(
|
|||
const_param_help,
|
||||
});
|
||||
diag.code(E0392);
|
||||
diag.emit()
|
||||
if item.kind.recovered() {
|
||||
// Silence potentially redundant error, as the item had a parse error.
|
||||
diag.delay_as_bug()
|
||||
} else {
|
||||
diag.emit()
|
||||
}
|
||||
}
|
||||
|
||||
/// Detects cases where an ADT/LTA is trivially cyclical -- we want to detect this so
|
||||
|
|
|
|||
|
|
@ -488,7 +488,30 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
}
|
||||
|
||||
let Some(InferSource { span, kind }) = local_visitor.infer_source else {
|
||||
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
|
||||
let silence = if let DefKind::AssocFn = self.tcx.def_kind(body_def_id)
|
||||
&& let parent = self.tcx.parent(body_def_id.into())
|
||||
&& self.tcx.is_automatically_derived(parent)
|
||||
&& let Some(parent) = parent.as_local()
|
||||
&& let hir::Node::Item(item) = self.tcx.hir_node_by_def_id(parent)
|
||||
&& let hir::ItemKind::Impl(imp) = item.kind
|
||||
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = imp.self_ty.kind
|
||||
&& let Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, def_id) = path.res
|
||||
&& let Some(def_id) = def_id.as_local()
|
||||
&& let hir::Node::Item(item) = self.tcx.hir_node_by_def_id(def_id)
|
||||
{
|
||||
// We have encountered an inference error within an automatically derived `impl`,
|
||||
// from a `#[derive(..)]` on an item that had a parse error. Because the parse
|
||||
// error might have caused the expanded code to be malformed, we silence the
|
||||
// inference error.
|
||||
item.kind.recovered()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let mut err = self.bad_inference_failure_err(failure_span, arg_data, error_code);
|
||||
if silence {
|
||||
err.downgrade_to_delayed_bug();
|
||||
}
|
||||
return err;
|
||||
};
|
||||
|
||||
let (source_kind, name, long_ty_path) = kind.ty_localized_msg(self);
|
||||
|
|
|
|||
14
tests/ui/structs/parse-error-with-type-param.fixed
Normal file
14
tests/ui/structs/parse-error-with-type-param.fixed
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
//@ run-rustfix
|
||||
// #141403
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Clone)]
|
||||
struct B<T> {
|
||||
a: A<(T, u32)>, // <- note, comma is missing here
|
||||
/// asdf
|
||||
//~^ ERROR found a documentation comment that doesn't document anything
|
||||
b: u32,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
struct A<T>(T);
|
||||
fn main() {}
|
||||
14
tests/ui/structs/parse-error-with-type-param.rs
Normal file
14
tests/ui/structs/parse-error-with-type-param.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
//@ run-rustfix
|
||||
// #141403
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Clone)]
|
||||
struct B<T> {
|
||||
a: A<(T, u32)> // <- note, comma is missing here
|
||||
/// asdf
|
||||
//~^ ERROR found a documentation comment that doesn't document anything
|
||||
b: u32,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
struct A<T>(T);
|
||||
fn main() {}
|
||||
18
tests/ui/structs/parse-error-with-type-param.stderr
Normal file
18
tests/ui/structs/parse-error-with-type-param.stderr
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
error[E0585]: found a documentation comment that doesn't document anything
|
||||
--> $DIR/parse-error-with-type-param.rs:8:5
|
||||
|
|
||||
LL | struct B<T> {
|
||||
| - while parsing this struct
|
||||
LL | a: A<(T, u32)> // <- note, comma is missing here
|
||||
LL | /// asdf
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= help: doc comments must come before what they document, if a comment was intended use `//`
|
||||
help: missing comma here
|
||||
|
|
||||
LL | a: A<(T, u32)>, // <- note, comma is missing here
|
||||
| +
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0585`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue