Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/structs/parse-error-with-type-param.fixed
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions tests/ui/structs/parse-error-with-type-param.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions tests/ui/structs/parse-error-with-type-param.stderr
Original file line number Diff line number Diff line change
@@ -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