Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constraint span when lowering associated types #63620

Merged
merged 2 commits into from
Aug 20, 2019
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
25 changes: 13 additions & 12 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use syntax::symbol::{kw, sym, Symbol};
use syntax::tokenstream::{TokenStream, TokenTree};
use syntax::parse::token::{self, Token};
use syntax::visit::{self, Visitor};
use syntax_pos::{DUMMY_SP, Span};
use syntax_pos::Span;

const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;

Expand Down Expand Up @@ -1033,13 +1033,14 @@ impl<'a> LoweringContext<'a> {
/// ```
///
/// returns a `hir::TypeBinding` representing `Item`.
fn lower_assoc_ty_constraint(&mut self,
c: &AssocTyConstraint,
itctx: ImplTraitContext<'_>)
-> hir::TypeBinding {
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", c, itctx);
fn lower_assoc_ty_constraint(
&mut self,
constraint: &AssocTyConstraint,
itctx: ImplTraitContext<'_>,
) -> hir::TypeBinding {
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);

let kind = match c.kind {
let kind = match constraint.kind {
AssocTyConstraintKind::Equality { ref ty } => hir::TypeBindingKind::Equality {
ty: self.lower_ty(ty, itctx)
},
Expand Down Expand Up @@ -1094,15 +1095,15 @@ impl<'a> LoweringContext<'a> {
impl_trait_node_id,
DefPathData::ImplTrait,
ExpnId::root(),
DUMMY_SP
constraint.span,
);

self.with_dyn_type_scope(false, |this| {
let ty = this.lower_ty(
&Ty {
id: this.sess.next_node_id(),
node: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
span: DUMMY_SP,
span: constraint.span,
},
itctx,
);
Expand All @@ -1124,10 +1125,10 @@ impl<'a> LoweringContext<'a> {
};

hir::TypeBinding {
hir_id: self.lower_node_id(c.id),
ident: c.ident,
hir_id: self.lower_node_id(constraint.id),
ident: constraint.ident,
kind,
span: c.span,
span: constraint.span,
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
) -> InferOk<'tcx, (T, OpaqueTypeMap<'tcx>)> {
debug!(
"instantiate_opaque_types(value={:?}, parent_def_id={:?}, body_id={:?}, \
param_env={:?})",
value, parent_def_id, body_id, param_env,
param_env={:?}, value_span={:?})",
value, parent_def_id, body_id, param_env, value_span,
);
let mut instantiator = Instantiator {
infcx: self,
Expand Down Expand Up @@ -1111,6 +1111,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
return opaque_defn.concrete_ty;
}
let span = tcx.def_span(def_id);
debug!("fold_opaque_ty {:?} {:?}", self.value_span, span);
let ty_var = infcx
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span });

Expand Down
21 changes: 12 additions & 9 deletions src/librustc/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
/// This is always inlined, despite its size, because it has a single
/// callsite and it is called *very* frequently.
#[inline(always)]
fn process_obligation(&mut self,
pending_obligation: &mut Self::Obligation)
-> ProcessResult<Self::Obligation, Self::Error>
{
fn process_obligation(
&mut self,
pending_obligation: &mut Self::Obligation,
) -> ProcessResult<Self::Obligation, Self::Error> {
// if we were stalled on some unresolved variables, first check
// whether any of them have been resolved; if not, don't bother
// doing more work yet
Expand All @@ -277,7 +277,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
self.selcx.infcx().resolve_vars_if_possible(&obligation.predicate);
}

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

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

ty::Predicate::WellFormed(ty) => {
match ty::wf::obligations(self.selcx.infcx(),
obligation.param_env,
obligation.cause.body_id,
ty, obligation.cause.span) {
match ty::wf::obligations(
self.selcx.infcx(),
obligation.param_env,
obligation.cause.body_id,
ty,
obligation.cause.span,
) {
None => {
pending_obligation.stalled_on = vec![ty];
ProcessResult::Unchanged
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This test documents that `type Out = Box<dyn Bar<Assoc: Copy>>;`
// is allowed and will correctly reject an opaque `type Out` which
// does not satisfy the bound `<TheType as Bar>::Assoc: Copy`.
//
// FIXME(rust-lang/lang): I think this behavior is logical if we want to allow
// `dyn Trait<Assoc: Bound>` but we should decide if we want that. // Centril
//
// Additionally, as reported in https://github.com/rust-lang/rust/issues/63594,
// we check that the spans for the error message are sane here.

#![feature(associated_type_bounds)]

fn main() {}

trait Bar { type Assoc; }

trait Thing {
type Out;
fn func() -> Self::Out;
}

struct AssocNoCopy;
impl Bar for AssocNoCopy { type Assoc = String; }

impl Thing for AssocNoCopy {
type Out = Box<dyn Bar<Assoc: Copy>>;
//~^ ERROR the trait bound `std::string::String: std::marker::Copy` is not satisfied

fn func() -> Self::Out {
Box::new(AssocNoCopy)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:26:28
|
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
| ^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
|
= note: the return type of a function must have a statically known size

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Loading