Rollup merge of #151082 - issue-141403, r=Kivooeo

Silence unused type param error on struct parse error

Given

```
#[derive(Clone)]
struct B<T> {
    a: A<(T, u32)> // <- note, comma is missing here
    /// asdf
    b: u32,
}
```

do not emit unnecessary "unused `T`" error.

Fix rust-lang/rust#141403.
This commit is contained in:
Jacob Pratt 2026-01-15 19:35:47 -05:00 committed by GitHub
commit 23f6cb5f12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 96 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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);

View 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() {}

View 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() {}

View 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`.