Skip to content

Commit 1671b9b

Browse files
committed
Auto merge of #26221 - sanxiyn:wf-span, r=arielb1
When checking field types of struct or enum definitions, use spans for field types instead of the entire item.
2 parents 4cc87c8 + 793d9fc commit 1671b9b

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/librustc_typeck/check/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1626,10 +1626,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16261626
let t = ast_ty_to_ty(self, self, ast_t);
16271627

16281628
let mut bounds_checker = wf::BoundsChecker::new(self,
1629-
ast_t.span,
16301629
self.body_id,
16311630
None);
1632-
bounds_checker.check_ty(t);
1631+
bounds_checker.check_ty(t, ast_t.span);
16331632

16341633
t
16351634
}

src/librustc_typeck/check/wf.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use util::ppaux::{Repr, UserString};
2323
use std::collections::HashSet;
2424
use syntax::ast;
2525
use syntax::ast_util::local_def;
26-
use syntax::codemap::Span;
26+
use syntax::codemap::{DUMMY_SP, Span};
2727
use syntax::parse::token::{self, special_idents};
2828
use syntax::visit;
2929
use syntax::visit::Visitor;
@@ -162,15 +162,14 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
162162
self.with_fcx(item, |this, fcx| {
163163
let variants = lookup_fields(fcx);
164164
let mut bounds_checker = BoundsChecker::new(fcx,
165-
item.span,
166165
item.id,
167166
Some(&mut this.cache));
168167
debug!("check_type_defn at bounds_checker.scope: {:?}", bounds_checker.scope);
169168

170-
for variant in &variants {
169+
for variant in &variants {
171170
for field in &variant.fields {
172171
// Regions are checked below.
173-
bounds_checker.check_traits_in_ty(field.ty);
172+
bounds_checker.check_traits_in_ty(field.ty, field.span);
174173
}
175174

176175
// For DST, all intermediate types must be sized.
@@ -199,7 +198,6 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
199198
{
200199
self.with_fcx(item, |this, fcx| {
201200
let mut bounds_checker = BoundsChecker::new(fcx,
202-
item.span,
203201
item.id,
204202
Some(&mut this.cache));
205203
debug!("check_item_type at bounds_checker.scope: {:?}", bounds_checker.scope);
@@ -209,7 +207,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
209207
&fcx.inh.param_env.free_substs,
210208
&type_scheme.ty);
211209

212-
bounds_checker.check_traits_in_ty(item_ty);
210+
bounds_checker.check_traits_in_ty(item_ty, item.span);
213211
});
214212
}
215213

@@ -218,7 +216,6 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
218216
{
219217
self.with_fcx(item, |this, fcx| {
220218
let mut bounds_checker = BoundsChecker::new(fcx,
221-
item.span,
222219
item.id,
223220
Some(&mut this.cache));
224221
debug!("check_impl at bounds_checker.scope: {:?}", bounds_checker.scope);
@@ -231,7 +228,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
231228
&fcx.inh.param_env.free_substs,
232229
&self_ty);
233230

234-
bounds_checker.check_traits_in_ty(self_ty);
231+
bounds_checker.check_traits_in_ty(self_ty, item.span);
235232

236233
// Similarly, obtain an "inside" reference to the trait
237234
// that the impl implements.
@@ -252,7 +249,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
252249
// trait reference. Instead, this is done at the impl site.
253250
// Arguably this is wrong and we should treat the trait-reference
254251
// the same way as we treat the self-type.
255-
bounds_checker.check_trait_ref(&trait_ref);
252+
bounds_checker.check_trait_ref(&trait_ref, item.span);
256253

257254
let cause =
258255
traits::ObligationCause::new(
@@ -483,11 +480,10 @@ pub struct BoundsChecker<'cx,'tcx:'cx> {
483480

484481
impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
485482
pub fn new(fcx: &'cx FnCtxt<'cx,'tcx>,
486-
span: Span,
487483
scope: ast::NodeId,
488484
cache: Option<&'cx mut HashSet<Ty<'tcx>>>)
489485
-> BoundsChecker<'cx,'tcx> {
490-
BoundsChecker { fcx: fcx, span: span, scope: scope,
486+
BoundsChecker { fcx: fcx, span: DUMMY_SP, scope: scope,
491487
cache: cache, binding_count: 0 }
492488
}
493489

@@ -500,30 +496,32 @@ impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
500496
///
501497
/// Note that it does not (currently, at least) check that `A : Copy` (that check is delegated
502498
/// to the point where impl `A : Trait<B>` is implemented).
503-
pub fn check_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
499+
pub fn check_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, span: Span) {
504500
let trait_predicates = ty::lookup_predicates(self.fcx.tcx(), trait_ref.def_id);
505501

506-
let bounds = self.fcx.instantiate_bounds(self.span,
502+
let bounds = self.fcx.instantiate_bounds(span,
507503
trait_ref.substs,
508504
&trait_predicates);
509505

510506
self.fcx.add_obligations_for_parameters(
511507
traits::ObligationCause::new(
512-
self.span,
508+
span,
513509
self.fcx.body_id,
514510
traits::ItemObligation(trait_ref.def_id)),
515511
&bounds);
516512

517513
for &ty in &trait_ref.substs.types {
518-
self.check_traits_in_ty(ty);
514+
self.check_traits_in_ty(ty, span);
519515
}
520516
}
521517

522-
pub fn check_ty(&mut self, ty: Ty<'tcx>) {
518+
pub fn check_ty(&mut self, ty: Ty<'tcx>, span: Span) {
519+
self.span = span;
523520
ty.fold_with(self);
524521
}
525522

526-
fn check_traits_in_ty(&mut self, ty: Ty<'tcx>) {
523+
fn check_traits_in_ty(&mut self, ty: Ty<'tcx>, span: Span) {
524+
self.span = span;
527525
// When checking types outside of a type def'n, we ignore
528526
// region obligations. See discussion below in fold_ty().
529527
self.binding_count += 1;

src/test/compile-fail/trait-bounds-on-structs-and-enums.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,27 @@ impl<T> Foo<T> {
3232
}
3333

3434
struct Baz {
35-
//~^ ERROR not implemented
36-
a: Foo<isize>,
35+
a: Foo<isize>, //~ ERROR not implemented
3736
}
3837

3938
enum Boo {
40-
//~^ ERROR not implemented
41-
Quux(Bar<usize>),
39+
Quux(Bar<usize>), //~ ERROR not implemented
4240
}
4341

4442
struct Badness<U> {
45-
//~^ ERROR not implemented
46-
b: Foo<U>,
43+
b: Foo<U>, //~ ERROR not implemented
4744
}
4845

4946
enum MoreBadness<V> {
50-
//~^ ERROR not implemented
51-
EvenMoreBadness(Bar<V>),
47+
EvenMoreBadness(Bar<V>), //~ ERROR not implemented
48+
}
49+
50+
struct TupleLike(
51+
Foo<i32>, //~ ERROR not implemented
52+
);
53+
54+
enum Enum {
55+
DictionaryLike { field: Bar<i32> }, //~ ERROR not implemented
5256
}
5357

5458
trait PolyTrait<T>

0 commit comments

Comments
 (0)