From 78761d64a990a58c7c99688717e77ed0ea477e2a Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Mon, 6 Jul 2015 18:46:03 +0300 Subject: [PATCH] don't use type_parameter_def during astconv astconv is called when converting the type-parameter, which leads to a crash. Fixes #26812. --- src/librustc_typeck/astconv.rs | 42 ++++++++++++++++++++-------- src/test/compile-fail/issue-26812.rs | 12 ++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 src/test/compile-fail/issue-26812.rs diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 7f29af0d84b..046e83bf3fc 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1113,6 +1113,7 @@ fn report_ambiguous_associated_type(tcx: &ty::ctxt, // any ambiguity. fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>, ty_param_node_id: ast::NodeId, + ty_param_name: Option, assoc_name: ast::Name, span: Span) -> Result, ErrorReported> @@ -1138,12 +1139,21 @@ fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>, .filter(|b| this.trait_defines_associated_type_named(b.def_id(), assoc_name)) .collect(); - let ty_param_name = tcx.type_parameter_def(ty_param_node_id).name; - one_bound_for_assoc_type(tcx, - suitable_bounds, - &token::get_name(ty_param_name), - &token::get_name(assoc_name), - span) + if let Some(s) = ty_param_name { + // borrowck doesn't like this any other way + one_bound_for_assoc_type(tcx, + suitable_bounds, + &token::get_name(s), + &token::get_name(assoc_name), + span) + } else { + one_bound_for_assoc_type(tcx, + suitable_bounds, + "Self", + &token::get_name(assoc_name), + span) + + } } @@ -1240,12 +1250,20 @@ fn associated_path_def_to_ty<'tcx>(this: &AstConv<'tcx>, _ => unreachable!() } } - (&ty::TyParam(_), def::DefTyParam(..)) | - (&ty::TyParam(_), def::DefSelfTy(Some(_), None)) => { - // A type parameter or Self, we need to find the associated item from - // a bound. - let ty_param_node_id = ty_path_def.local_node_id(); - match find_bound_for_assoc_item(this, ty_param_node_id, assoc_name, span) { + (&ty::TyParam(_), def::DefSelfTy(Some(trait_did), None)) => { + assert_eq!(trait_did.krate, ast::LOCAL_CRATE); + match find_bound_for_assoc_item(this, trait_did.node, None, assoc_name, span) { + Ok(bound) => bound, + Err(ErrorReported) => return (tcx.types.err, ty_path_def), + } + } + (&ty::TyParam(_), def::DefTyParam(_, _, param_did, param_name)) => { + assert_eq!(param_did.krate, ast::LOCAL_CRATE); + match find_bound_for_assoc_item(this, + param_did.node, + Some(param_name), + assoc_name, + span) { Ok(bound) => bound, Err(ErrorReported) => return (tcx.types.err, ty_path_def), } diff --git a/src/test/compile-fail/issue-26812.rs b/src/test/compile-fail/issue-26812.rs new file mode 100644 index 00000000000..c1ccfe269cd --- /dev/null +++ b/src/test/compile-fail/issue-26812.rs @@ -0,0 +1,12 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn avg(_: T) {} //~ ERROR associated type `Item` not found for `T` +fn main() {}