Skip to content

Commit af7f8f9

Browse files
committed
Silence follow up errors if astconv already errored
1 parent 252ac15 commit af7f8f9

21 files changed

+56
-164
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
390390
infer_args,
391391
);
392392

393+
if let Err(err) = &arg_count.correct
394+
&& let Some(reported) = err.reported
395+
{
396+
self.set_tainted_by_errors(reported);
397+
}
398+
393399
// Skip processing if type has no generic parameters.
394400
// Traits always have `Self` as a generic parameter, which means they will not return early
395401
// here and so associated type bindings will be handled regardless of whether there are any
@@ -568,6 +574,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
568574
span,
569575
modifier: constness.as_str(),
570576
});
577+
self.set_tainted_by_errors(e);
571578
arg_count.correct =
572579
Err(GenericArgCountMismatch { reported: Some(e), invalid_args: vec![] });
573580
}

compiler/rustc_hir_analysis/src/collect.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_target::spec::abi;
3535
use rustc_trait_selection::infer::InferCtxtExt;
3636
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
3737
use rustc_trait_selection::traits::ObligationCtxt;
38+
use std::cell::Cell;
3839
use std::iter;
3940
use std::ops::Bound;
4041

@@ -119,6 +120,7 @@ pub fn provide(providers: &mut Providers) {
119120
pub struct ItemCtxt<'tcx> {
120121
tcx: TyCtxt<'tcx>,
121122
item_def_id: LocalDefId,
123+
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
122124
}
123125

124126
///////////////////////////////////////////////////////////////////////////
@@ -343,7 +345,7 @@ fn bad_placeholder<'tcx>(
343345

344346
impl<'tcx> ItemCtxt<'tcx> {
345347
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
346-
ItemCtxt { tcx, item_def_id }
348+
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
347349
}
348350

349351
pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
@@ -357,6 +359,13 @@ impl<'tcx> ItemCtxt<'tcx> {
357359
pub fn node(&self) -> hir::Node<'tcx> {
358360
self.tcx.hir_node(self.hir_id())
359361
}
362+
363+
fn check_tainted_by_errors(&self) -> Result<(), ErrorGuaranteed> {
364+
match self.tainted_by_errors.get() {
365+
Some(err) => Err(err),
366+
None => Ok(()),
367+
}
368+
}
360369
}
361370

362371
impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
@@ -492,8 +501,8 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
492501
ty.ty_adt_def()
493502
}
494503

495-
fn set_tainted_by_errors(&self, _: ErrorGuaranteed) {
496-
// There's no obvious place to track this, so just let it go.
504+
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
505+
self.tainted_by_errors.set(Some(err));
497506
}
498507

499508
fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
513513
bug!("unexpected sort of node in type_of(): {:?}", x);
514514
}
515515
};
516-
ty::EarlyBinder::bind(output)
516+
if let Err(e) = icx.check_tainted_by_errors() {
517+
ty::EarlyBinder::bind(Ty::new_error(tcx, e))
518+
} else {
519+
ty::EarlyBinder::bind(output)
520+
}
517521
}
518522

519523
pub(super) fn type_of_opaque(

tests/ui/associated-inherent-types/issue-109071.no_gate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | type Item = &[T];
3030
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
3131

3232
error[E0223]: ambiguous associated type
33-
--> $DIR/issue-109071.rs:16:22
33+
--> $DIR/issue-109071.rs:15:22
3434
|
3535
LL | fn T() -> Option<Self::Item> {}
3636
| ^^^^^^^^^^

tests/ui/associated-inherent-types/issue-109071.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ impl<T> Windows { //~ ERROR: missing generics for struct `Windows`
99
//[no_gate]~^ ERROR: inherent associated types are unstable
1010

1111
fn next() -> Option<Self::Item> {}
12-
//[with_gate]~^ ERROR type annotations needed
1312
}
1413

1514
impl<T> Windows<T> {
1615
fn T() -> Option<Self::Item> {}
17-
//[no_gate]~^ ERROR: ambiguous associated type
18-
//[with_gate]~^^ ERROR type annotations needed
16+
//~^ ERROR: ambiguous associated type
1917
}
2018

2119
fn main() {}

tests/ui/associated-inherent-types/issue-109071.with_gate.stderr

+12-11
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ help: add missing generic argument
2020
LL | impl<T> Windows<T> {
2121
| +++
2222

23-
error[E0282]: type annotations needed
24-
--> $DIR/issue-109071.rs:11:18
25-
|
26-
LL | fn next() -> Option<Self::Item> {}
27-
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
28-
29-
error[E0282]: type annotations needed
30-
--> $DIR/issue-109071.rs:16:15
23+
error[E0223]: ambiguous associated type
24+
--> $DIR/issue-109071.rs:15:22
3125
|
3226
LL | fn T() -> Option<Self::Item> {}
33-
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
27+
| ^^^^^^^^^^
28+
|
29+
help: use fully-qualified syntax
30+
|
31+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
32+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
34+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3435

35-
error: aborting due to 4 previous errors
36+
error: aborting due to 3 previous errors
3637

37-
Some errors have detailed explanations: E0107, E0282, E0637.
38+
Some errors have detailed explanations: E0107, E0223, E0637.
3839
For more information about an error, try `rustc --explain E0107`.

tests/ui/const-generics/issues/issue-71381.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "
77
= note: type parameters may not be used in the type of const parameters
88

99
error[E0770]: the type of const parameters must not depend on other generic parameters
10-
--> $DIR/issue-71381.rs:23:40
10+
--> $DIR/issue-71381.rs:22:40
1111
|
1212
LL | const FN: unsafe extern "C" fn(Args),
1313
| ^^^^ the type must not depend on the parameter `Args`

tests/ui/const-generics/issues/issue-71381.min.stderr

+2-18
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,13 @@ LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "
77
= note: type parameters may not be used in the type of const parameters
88

99
error[E0770]: the type of const parameters must not depend on other generic parameters
10-
--> $DIR/issue-71381.rs:23:40
10+
--> $DIR/issue-71381.rs:22:40
1111
|
1212
LL | const FN: unsafe extern "C" fn(Args),
1313
| ^^^^ the type must not depend on the parameter `Args`
1414
|
1515
= note: type parameters may not be used in the type of const parameters
1616

17-
error: using function pointers as const generic parameters is forbidden
18-
--> $DIR/issue-71381.rs:14:61
19-
|
20-
LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
22-
|
23-
= note: the only supported types are integers, `bool` and `char`
24-
25-
error: using function pointers as const generic parameters is forbidden
26-
--> $DIR/issue-71381.rs:23:19
27-
|
28-
LL | const FN: unsafe extern "C" fn(Args),
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
|
31-
= note: the only supported types are integers, `bool` and `char`
32-
33-
error: aborting due to 4 previous errors
17+
error: aborting due to 2 previous errors
3418

3519
For more information about this error, try `rustc --explain E0770`.

tests/ui/const-generics/issues/issue-71381.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ unsafe extern "C" fn pass(args: PassArg) {
1212

1313
impl Test {
1414
pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) {
15-
//[min]~^ ERROR: using function pointers as const generic parameters is forbidden
16-
//~^^ ERROR: the type of const parameters must not depend on other generic parameters
15+
//~^ ERROR: the type of const parameters must not depend on other generic parameters
1716
self.0 = Self::trampiline::<Args, IDX, FN> as _
1817
}
1918

2019
unsafe extern "C" fn trampiline<
2120
Args: Sized,
2221
const IDX: usize,
2322
const FN: unsafe extern "C" fn(Args),
24-
//[min]~^ ERROR: using function pointers as const generic parameters is forbidden
25-
//~^^ ERROR: the type of const parameters must not depend on other generic parameters
23+
//~^ ERROR: the type of const parameters must not depend on other generic parameters
2624
>(
2725
args: Args,
2826
) {

tests/ui/const-generics/issues/issue-71611.min.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ LL | fn func<A, const F: fn(inner: A)>(outer: A) {
66
|
77
= note: type parameters may not be used in the type of const parameters
88

9-
error: using function pointers as const generic parameters is forbidden
10-
--> $DIR/issue-71611.rs:5:21
11-
|
12-
LL | fn func<A, const F: fn(inner: A)>(outer: A) {
13-
| ^^^^^^^^^^^^
14-
|
15-
= note: the only supported types are integers, `bool` and `char`
16-
17-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
1810

1911
For more information about this error, try `rustc --explain E0770`.

tests/ui/const-generics/issues/issue-71611.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#![cfg_attr(full, allow(incomplete_features))]
44

55
fn func<A, const F: fn(inner: A)>(outer: A) {
6-
//[min]~^ ERROR: using function pointers as const generic parameters is forbidden
7-
//~^^ ERROR: the type of const parameters must not depend on other generic parameters
6+
//~^ ERROR: the type of const parameters must not depend on other generic parameters
87
F(outer);
98
}
109

tests/ui/generic-associated-types/issue-71176.rs

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ impl Provider for () {
99
struct Holder<B> {
1010
inner: Box<dyn Provider<A = B>>,
1111
//~^ ERROR: missing generics for associated type
12-
//~| ERROR: missing generics for associated type
13-
//~| ERROR: missing generics for associated type
14-
//~| ERROR: the trait `Provider` cannot be made into an object
1512
}
1613

1714
fn main() {

tests/ui/generic-associated-types/issue-71176.stderr

+2-53
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,6 @@ help: add missing lifetime argument
1414
LL | inner: Box<dyn Provider<A<'a> = B>>,
1515
| ++++
1616

17-
error[E0107]: missing generics for associated type `Provider::A`
18-
--> $DIR/issue-71176.rs:10:27
19-
|
20-
LL | inner: Box<dyn Provider<A = B>>,
21-
| ^ expected 1 lifetime argument
22-
|
23-
note: associated type defined here, with 1 lifetime parameter: `'a`
24-
--> $DIR/issue-71176.rs:2:10
25-
|
26-
LL | type A<'a>;
27-
| ^ --
28-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
29-
help: add missing lifetime argument
30-
|
31-
LL | inner: Box<dyn Provider<A<'a> = B>>,
32-
| ++++
33-
34-
error[E0107]: missing generics for associated type `Provider::A`
35-
--> $DIR/issue-71176.rs:10:27
36-
|
37-
LL | inner: Box<dyn Provider<A = B>>,
38-
| ^ expected 1 lifetime argument
39-
|
40-
note: associated type defined here, with 1 lifetime parameter: `'a`
41-
--> $DIR/issue-71176.rs:2:10
42-
|
43-
LL | type A<'a>;
44-
| ^ --
45-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
46-
help: add missing lifetime argument
47-
|
48-
LL | inner: Box<dyn Provider<A<'a> = B>>,
49-
| ++++
50-
51-
error[E0038]: the trait `Provider` cannot be made into an object
52-
--> $DIR/issue-71176.rs:10:14
53-
|
54-
LL | inner: Box<dyn Provider<A = B>>,
55-
| ^^^^^^^^^^^^^^^^^^^ `Provider` cannot be made into an object
56-
|
57-
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
58-
--> $DIR/issue-71176.rs:2:10
59-
|
60-
LL | trait Provider {
61-
| -------- this trait cannot be made into an object...
62-
LL | type A<'a>;
63-
| ^ ...because it contains the generic associated type `A`
64-
= help: consider moving `A` to another trait
65-
= help: only type `()` implements the trait, consider using it directly instead
66-
67-
error: aborting due to 4 previous errors
17+
error: aborting due to 1 previous error
6818

69-
Some errors have detailed explanations: E0038, E0107.
70-
For more information about an error, try `rustc --explain E0038`.
19+
For more information about this error, try `rustc --explain E0107`.

tests/ui/issues/issue-3214.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn foo<T>() {
55

66
impl<T> Drop for Foo<T> {
77
//~^ ERROR struct takes 0 generic arguments but 1 generic argument
8-
//~| ERROR `T` is not constrained
98
fn drop(&mut self) {}
109
}
1110
}

tests/ui/issues/issue-3214.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ note: struct defined here, with 0 generic parameters
2222
LL | struct Foo {
2323
| ^^^
2424

25-
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
26-
--> $DIR/issue-3214.rs:6:10
27-
|
28-
LL | impl<T> Drop for Foo<T> {
29-
| ^ unconstrained type parameter
30-
31-
error: aborting due to 3 previous errors
25+
error: aborting due to 2 previous errors
3226

33-
Some errors have detailed explanations: E0107, E0207, E0401.
27+
Some errors have detailed explanations: E0107, E0401.
3428
For more information about an error, try `rustc --explain E0107`.

tests/ui/layout/issue-84108.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ static FOO: (dyn AsRef<OsStr>, u8) = ("hello", 42);
88

99
const BAR: (&Path, [u8], usize) = ("hello", [], 42);
1010
//~^ ERROR cannot find type `Path` in this scope
11-
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
12-
//~| ERROR mismatched types
1311

1412
static BAZ: ([u8], usize) = ([], 0);
1513
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time

tests/ui/layout/issue-84108.stderr

+3-21
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,7 @@ LL + use std::path::Path;
2121
|
2222

2323
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
24-
--> $DIR/issue-84108.rs:9:12
25-
|
26-
LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
27-
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
28-
|
29-
= help: the trait `Sized` is not implemented for `[u8]`
30-
= note: only the last element of a tuple may have a dynamically sized type
31-
32-
error[E0308]: mismatched types
33-
--> $DIR/issue-84108.rs:9:45
34-
|
35-
LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
36-
| ^^ expected `[u8]`, found `[_; 0]`
37-
|
38-
= note: expected slice `[u8]`
39-
found array `[_; 0]`
40-
41-
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
42-
--> $DIR/issue-84108.rs:14:13
24+
--> $DIR/issue-84108.rs:12:13
4325
|
4426
LL | static BAZ: ([u8], usize) = ([], 0);
4527
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -48,15 +30,15 @@ LL | static BAZ: ([u8], usize) = ([], 0);
4830
= note: only the last element of a tuple may have a dynamically sized type
4931

5032
error[E0308]: mismatched types
51-
--> $DIR/issue-84108.rs:14:30
33+
--> $DIR/issue-84108.rs:12:30
5234
|
5335
LL | static BAZ: ([u8], usize) = ([], 0);
5436
| ^^ expected `[u8]`, found `[_; 0]`
5537
|
5638
= note: expected slice `[u8]`
5739
found array `[_; 0]`
5840

59-
error: aborting due to 6 previous errors
41+
error: aborting due to 4 previous errors
6042

6143
Some errors have detailed explanations: E0277, E0308, E0412.
6244
For more information about an error, try `rustc --explain E0277`.

tests/ui/parser/issues/issue-103748-ICE-wrong-braces.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ struct Apple((Apple, Option(Banana ? Citron)));
55
//~| ERROR expected one of `)` or `,`, found `Citron`
66
//~| ERROR cannot find type `Citron` in this scope [E0412]
77
//~| ERROR parenthesized type parameters may only be used with a `Fn` trait [E0214]
8-
//~| ERROR recursive type `Apple` has infinite size [E0072]

0 commit comments

Comments
 (0)