Skip to content

Commit d648a16

Browse files
authored
Auto merge of #35130 - sanxiyn:unused-type-parameter-error, r=nrc
Suppress unused type parameter error when type has error field Fix #35075.
2 parents 535cea0 + 0365215 commit d648a16

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/librustc/ty/util.rs

+15
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
182182
pat_util::arm_contains_ref_binding(arm)
183183
}
184184

185+
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
186+
match ty.sty {
187+
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
188+
for field in def.all_fields() {
189+
let field_ty = field.ty(self, substs);
190+
if let TyError = field_ty.sty {
191+
return true;
192+
}
193+
}
194+
}
195+
_ => ()
196+
}
197+
false
198+
}
199+
185200
/// Returns the type of element at index `i` in tuple or tuple-like type `t`.
186201
/// For an enum `t`, `variant` is None only if `t` is a univariant enum.
187202
pub fn positional_element_ty(self,

src/librustc_typeck/check/wfcheck.rs

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
454454
item: &hir::Item,
455455
ast_generics: &hir::Generics)
456456
{
457+
let ty = self.tcx().node_id_to_type(item.id);
458+
if self.tcx().has_error_field(ty) {
459+
return;
460+
}
461+
457462
let item_def_id = self.tcx().map.local_def_id(item.id);
458463
let ty_predicates = self.tcx().lookup_predicates(item_def_id);
459464
let variances = self.tcx().item_variances(item_def_id);

src/test/compile-fail/issue-35075.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Bar<T> {
12+
inner: Foo<T> //~ ERROR type name `Foo` is undefined or not in scope
13+
}
14+
15+
enum Baz<T> {
16+
Foo(Foo<T>) //~ ERROR type name `Foo` is undefined or not in scope
17+
}
18+
19+
fn main() {}

src/test/compile-fail/resolve-type-param-in-item-in-trait.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,30 @@
1313
// scope (in this case, the enum).
1414

1515
trait TraitA<A> {
16-
fn outer(self) {
16+
fn outer(&self) {
1717
enum Foo<B> {
18-
//~^ ERROR parameter `B` is never used
1918
Variance(A)
2019
//~^ ERROR can't use type parameters from outer function
2120
}
2221
}
2322
}
2423

2524
trait TraitB<A> {
26-
fn outer(self) {
25+
fn outer(&self) {
2726
struct Foo<B>(A);
2827
//~^ ERROR can't use type parameters from outer function
29-
//~^^ ERROR parameter `B` is never used
3028
}
3129
}
3230

3331
trait TraitC<A> {
34-
fn outer(self) {
32+
fn outer(&self) {
3533
struct Foo<B> { a: A }
3634
//~^ ERROR can't use type parameters from outer function
37-
//~^^ ERROR parameter `B` is never used
3835
}
3936
}
4037

4138
trait TraitD<A> {
42-
fn outer(self) {
39+
fn outer(&self) {
4340
fn foo<B>(a: A) { }
4441
//~^ ERROR can't use type parameters from outer function
4542
}

0 commit comments

Comments
 (0)