Skip to content

Inference failure with for-loop and associated type that contains lifetimes #22066

Closed
@cessen

Description

@cessen

System Info:

Xubuntu 13.10
rustc 1.0.0-nightly (d3732a1 2015-02-06 23:30:17 +0000)
binary: rustc
commit-hash: d3732a1
commit-date: 2015-02-06 23:30:17 +0000
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

I'm trying to create a trait for formatting lines of text (e.g. do line wrapping, manage font size, etc.). I want implementers to only have to provide an iterator and a method for producing those iterators, and everything else is provided with default implementations that utilize those.

The following is a minified version of what I'm trying to do that produced an error.

Code:

pub trait LineFormatter<'a> {
    type Iter: Iterator<Item=&'a str> + 'a;
    fn iter(&'a self, line: &'a str) -> Self::Iter;

    fn dimensions(&'a self, line: &'a str) {
        for grapheme in self.iter(line) {
            // Do some stuff
        }
    }
}

fn main() {
    println!("Hello world!");
}

Error:

error: unable to infer enough type information about `_`; type annotations required [E0282]
src/main.rs:6         for grapheme in self.iter(line) {
                          ^~~~~~~~

This error seems strange enough on its own, because rustc ought to be able to infer the type of grapheme from the return type of Self::Iter::next(). So I thought perhaps the problem is that it somehow lacked type information about self.iter(). Thus I changed the code so that the iterator type is explicit, and got a different error:

Code:

pub trait LineFormatter<'a> {
    type Iter: Iterator<Item=&'a str> + 'a;
    fn iter(&'a self, line: &'a str) -> Self::Iter;

    fn dimensions(&'a self, line: &'a str) {
        let mut iter: Self::Iter = self.iter(line);
        for grapheme in iter {
            // Do some stuff
        }
    }
}

fn main() {
    println!("Hello world!");
}

Error (with backtrace):

error: internal compiler error: cannot relate bound region: ReFree(39, BrNamed(DefId { krate: 0, node: 66 }, 'a)) <= ReEarlyBound(66, TypeSpace, 0, 'a)
src/main.rs:6         let mut iter: Self::Iter = self.iter(line);
                                    ^~~~~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:129

stack backtrace:
   1:     0x7fac8cdf4250 - sys::backtrace::write::h361f9c7ed16d6f6eUsy
   2:     0x7fac8ce17950 - failure::on_fail::h69f5f434699d11ccOWF
   3:     0x7fac8cd777d0 - rt::unwind::begin_unwind_inner::hc4455d84b00b874blBF
   4:     0x7fac8a142040 - rt::unwind::begin_unwind::h13236156776379767181
   5:     0x7fac8a141fd0 - diagnostic::SpanHandler::span_bug::h5ec31613fa5cf53fBUE
   6:     0x7fac8af3d770 - middle::infer::region_inference::RegionVarBindings<'a, 'tcx>::make_subregion::h5d6daa2e030d47a0rqv
   7:     0x7fac8aee07f0 - middle::infer::region_inference::RegionVarBindings<'a, 'tcx>::make_eqregion::hc14a7b534b218055Qpv
   8:     0x7fac8aee04c0 - middle::infer::equate::Equate<'f, 'tcx>.Combine<'tcx>::regions::h7b601a41d445840dl9p
   9:     0x7fac8aeed100 - middle::infer::combine::Combine::substs_variances::h505559559396905773
  10:     0x7fac8aeed040 - middle::infer::combine::Combine::substs::h14093085151659474206
  11:     0x7fac8aeecd80 - middle::infer::combine::Combine::trait_refs::h11806062172857789792
  12:     0x7fac8aee83b0 - middle::infer::higher_ranked::C.HigherRankedRelations<'tcx>::higher_ranked_sub::h11345065735083413059
  13:     0x7fac8af5a350 - middle::infer::InferCtxt<'a, 'tcx>::sub_poly_trait_refs::h778d518607558f21afA
  14:     0x7fac8aff14c0 - middle::traits::select::SelectionContext<'cx, 'tcx>::match_poly_trait_ref::hf672789565982957nXS
  15:     0x7fac8aff0e80 - middle::traits::select::SelectionContext<'cx, 'tcx>::evaluate_where_clause::closure.78472
  16:     0x7fac8aff0850 - middle::traits::select::SelectionContext<'cx, 'tcx>::evaluate_where_clause::hd8fd18d51d7963f09pR
  17:     0x7fac8aff0d00 - iter::Filter<I, P>.Iterator::next::h666365327850076025
  18:     0x7fac8aff0a50 - vec::Vec<T>.Extend<T>::extend::h12082665635362077775
  19:     0x7fac8afeb4c0 - middle::traits::select::SelectionContext<'cx, 'tcx>::assemble_candidates::he7da8b218a200b5000Q
  20:     0x7fac8afde060 - middle::traits::select::SelectionContext<'cx, 'tcx>::candidate_from_obligation::hdd5f5f479a261580bFQ
  21:     0x7fac8afc3260 - middle::traits::select::SelectionContext<'cx, 'tcx>::select::h505d79adb1bd97bb3hQ
  22:     0x7fac8afd1a10 - middle::traits::project::assemble_candidates_from_impls::hf1079e75d7f576a9xfP
  23:     0x7fac8afcdb80 - middle::traits::project::opt_normalize_projection_type::hcd0d88e52c938941ZOO
  24:     0x7fac8afbabc0 - middle::traits::project::normalize_projection_type::ha0d2856f827aa82cHNO
  25:     0x7fac8afba380 - middle::traits::fulfill::FulfillmentContext<'tcx>::normalize_projection_type::h56c4a230d5438853zYN
  26:     0x7fac8c569df0 - check::FnCtxt<'a, 'tcx>::normalize_associated_type::h4c37786edcd185bfd7n
  27:     0x7fac8c5429b0 - check::FnCtxt<'a, 'tcx>.AstConv<'tcx>::projected_ty_from_poly_trait_ref::h26f1885bfd9232a3tMn
  28:     0x7fac8c5ce9d0 - astconv::ast_ty_to_ty::closure.33365
  29:     0x7fac8c56c8b0 - astconv::ast_ty_to_ty::hde8725d1f33fc37dS5u
  30:     0x7fac8c560150 - check::GatherLocalsVisitor<'a, 'tcx>.Visitor<'tcx>::visit_local::h7cca3eba209aa7564Jm
  31:     0x7fac8c542e10 - check::check_fn::hc7c6534eca9dcbb42Qm
  32:     0x7fac8c55eb70 - check::check_bare_fn::hc4a0a43726ab3cfekGm
  33:     0x7fac8c562a30 - check::check_method_body::h6885088892563788gen
  34:     0x7fac8c556470 - check::check_item::hf0f84b92a48dd09eBZm
  35:     0x7fac8c622530 - check_crate::closure.34477
  36:     0x7fac8c61ce00 - check_crate::h8571887ed583eda9PKA
  37:     0x7fac8d3e6b20 - driver::phase_3_run_analysis_passes::h8f5a5e5500230bd1oGa
  38:     0x7fac8d3cca40 - driver::compile_input::hcb43bced2a0b021bCba
  39:     0x7fac8d49ebe0 - run_compiler::hb81cf958c2f963666ac
  40:     0x7fac8d49d270 - thunk::F.Invoke<A, R>::invoke::h6159218808669033746
  41:     0x7fac8d49c1a0 - rt::unwind::try::try_fn::h8268217050585033268
  42:     0x7fac8ce83a90 - rust_try_inner
  43:     0x7fac8ce83a80 - rust_try
  44:     0x7fac8d49c450 - thunk::F.Invoke<A, R>::invoke::h8703575304585054118
  45:     0x7fac8ce03e40 - sys::thread::thread_start::h35f66aa7ce5d7277LCB
  46:     0x7fac86e44ea0 - start_thread
  47:     0x7fac8c9f1999 - __clone
  48:                0x0 - <unknown>

I wouldn't be surprised if I'm implementing this stuff wrong, but since it's producing an ICE it seemed like I ought to submit a bug report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions