Skip to content

Commit a2080a6

Browse files
authored
Rollup merge of #63620 - estebank:assoc-type-span, r=Centril
Use constraint span when lowering associated types Fix #63594. r? @Centril
2 parents bece117 + 1808e4d commit a2080a6

File tree

9 files changed

+376
-200
lines changed

9 files changed

+376
-200
lines changed

src/librustc/hir/lowering.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use syntax::symbol::{kw, sym, Symbol};
7272
use syntax::tokenstream::{TokenStream, TokenTree};
7373
use syntax::parse::token::{self, Token};
7474
use syntax::visit::{self, Visitor};
75-
use syntax_pos::{DUMMY_SP, Span};
75+
use syntax_pos::Span;
7676

7777
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
7878

@@ -1039,13 +1039,14 @@ impl<'a> LoweringContext<'a> {
10391039
/// ```
10401040
///
10411041
/// returns a `hir::TypeBinding` representing `Item`.
1042-
fn lower_assoc_ty_constraint(&mut self,
1043-
c: &AssocTyConstraint,
1044-
itctx: ImplTraitContext<'_>)
1045-
-> hir::TypeBinding {
1046-
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", c, itctx);
1042+
fn lower_assoc_ty_constraint(
1043+
&mut self,
1044+
constraint: &AssocTyConstraint,
1045+
itctx: ImplTraitContext<'_>,
1046+
) -> hir::TypeBinding {
1047+
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
10471048

1048-
let kind = match c.kind {
1049+
let kind = match constraint.kind {
10491050
AssocTyConstraintKind::Equality { ref ty } => hir::TypeBindingKind::Equality {
10501051
ty: self.lower_ty(ty, itctx)
10511052
},
@@ -1100,15 +1101,15 @@ impl<'a> LoweringContext<'a> {
11001101
impl_trait_node_id,
11011102
DefPathData::ImplTrait,
11021103
ExpnId::root(),
1103-
DUMMY_SP
1104+
constraint.span,
11041105
);
11051106

11061107
self.with_dyn_type_scope(false, |this| {
11071108
let ty = this.lower_ty(
11081109
&Ty {
11091110
id: this.sess.next_node_id(),
11101111
node: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1111-
span: DUMMY_SP,
1112+
span: constraint.span,
11121113
},
11131114
itctx,
11141115
);
@@ -1130,10 +1131,10 @@ impl<'a> LoweringContext<'a> {
11301131
};
11311132

11321133
hir::TypeBinding {
1133-
hir_id: self.lower_node_id(c.id),
1134-
ident: c.ident,
1134+
hir_id: self.lower_node_id(constraint.id),
1135+
ident: constraint.ident,
11351136
kind,
1136-
span: c.span,
1137+
span: constraint.span,
11371138
}
11381139
}
11391140

src/librustc/infer/opaque_types/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
127127
) -> InferOk<'tcx, (T, OpaqueTypeMap<'tcx>)> {
128128
debug!(
129129
"instantiate_opaque_types(value={:?}, parent_def_id={:?}, body_id={:?}, \
130-
param_env={:?})",
131-
value, parent_def_id, body_id, param_env,
130+
param_env={:?}, value_span={:?})",
131+
value, parent_def_id, body_id, param_env, value_span,
132132
);
133133
let mut instantiator = Instantiator {
134134
infcx: self,
@@ -1112,6 +1112,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
11121112
return opaque_defn.concrete_ty;
11131113
}
11141114
let span = tcx.def_span(def_id);
1115+
debug!("fold_opaque_ty {:?} {:?}", self.value_span, span);
11151116
let ty_var = infcx
11161117
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span });
11171118

src/librustc/traits/fulfill.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
248248
/// This is always inlined, despite its size, because it has a single
249249
/// callsite and it is called *very* frequently.
250250
#[inline(always)]
251-
fn process_obligation(&mut self,
252-
pending_obligation: &mut Self::Obligation)
253-
-> ProcessResult<Self::Obligation, Self::Error>
254-
{
251+
fn process_obligation(
252+
&mut self,
253+
pending_obligation: &mut Self::Obligation,
254+
) -> ProcessResult<Self::Obligation, Self::Error> {
255255
// if we were stalled on some unresolved variables, first check
256256
// whether any of them have been resolved; if not, don't bother
257257
// doing more work yet
@@ -277,7 +277,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
277277
self.selcx.infcx().resolve_vars_if_possible(&obligation.predicate);
278278
}
279279

280-
debug!("process_obligation: obligation = {:?}", obligation);
280+
debug!("process_obligation: obligation = {:?} cause = {:?}", obligation, obligation.cause);
281281

282282
match obligation.predicate {
283283
ty::Predicate::Trait(ref data) => {
@@ -425,10 +425,13 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
425425
}
426426

427427
ty::Predicate::WellFormed(ty) => {
428-
match ty::wf::obligations(self.selcx.infcx(),
429-
obligation.param_env,
430-
obligation.cause.body_id,
431-
ty, obligation.cause.span) {
428+
match ty::wf::obligations(
429+
self.selcx.infcx(),
430+
obligation.param_env,
431+
obligation.cause.body_id,
432+
ty,
433+
obligation.cause.span,
434+
) {
432435
None => {
433436
pending_obligation.stalled_on = vec![ty];
434437
ProcessResult::Unchanged
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This test documents that `type Out = Box<dyn Bar<Assoc: Copy>>;`
2+
// is allowed and will correctly reject an opaque `type Out` which
3+
// does not satisfy the bound `<TheType as Bar>::Assoc: Copy`.
4+
//
5+
// FIXME(rust-lang/lang): I think this behavior is logical if we want to allow
6+
// `dyn Trait<Assoc: Bound>` but we should decide if we want that. // Centril
7+
//
8+
// Additionally, as reported in https://github.com/rust-lang/rust/issues/63594,
9+
// we check that the spans for the error message are sane here.
10+
11+
#![feature(associated_type_bounds)]
12+
13+
fn main() {}
14+
15+
trait Bar { type Assoc; }
16+
17+
trait Thing {
18+
type Out;
19+
fn func() -> Self::Out;
20+
}
21+
22+
struct AssocNoCopy;
23+
impl Bar for AssocNoCopy { type Assoc = String; }
24+
25+
impl Thing for AssocNoCopy {
26+
type Out = Box<dyn Bar<Assoc: Copy>>;
27+
//~^ ERROR the trait bound `std::string::String: std::marker::Copy` is not satisfied
28+
29+
fn func() -> Self::Out {
30+
Box::new(AssocNoCopy)
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
2+
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:26:28
3+
|
4+
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
5+
| ^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
6+
|
7+
= note: the return type of a function must have a statically known size
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)