Skip to content

Commit 78761d6

Browse files
author
Ariel Ben-Yehuda
committed
don't use type_parameter_def during astconv
astconv is called when converting the type-parameter, which leads to a crash. Fixes #26812.
1 parent a2b927c commit 78761d6

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/librustc_typeck/astconv.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ fn report_ambiguous_associated_type(tcx: &ty::ctxt,
11131113
// any ambiguity.
11141114
fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>,
11151115
ty_param_node_id: ast::NodeId,
1116+
ty_param_name: Option<ast::Name>,
11161117
assoc_name: ast::Name,
11171118
span: Span)
11181119
-> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
@@ -1138,12 +1139,21 @@ fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>,
11381139
.filter(|b| this.trait_defines_associated_type_named(b.def_id(), assoc_name))
11391140
.collect();
11401141

1141-
let ty_param_name = tcx.type_parameter_def(ty_param_node_id).name;
1142-
one_bound_for_assoc_type(tcx,
1143-
suitable_bounds,
1144-
&token::get_name(ty_param_name),
1145-
&token::get_name(assoc_name),
1146-
span)
1142+
if let Some(s) = ty_param_name {
1143+
// borrowck doesn't like this any other way
1144+
one_bound_for_assoc_type(tcx,
1145+
suitable_bounds,
1146+
&token::get_name(s),
1147+
&token::get_name(assoc_name),
1148+
span)
1149+
} else {
1150+
one_bound_for_assoc_type(tcx,
1151+
suitable_bounds,
1152+
"Self",
1153+
&token::get_name(assoc_name),
1154+
span)
1155+
1156+
}
11471157
}
11481158

11491159

@@ -1240,12 +1250,20 @@ fn associated_path_def_to_ty<'tcx>(this: &AstConv<'tcx>,
12401250
_ => unreachable!()
12411251
}
12421252
}
1243-
(&ty::TyParam(_), def::DefTyParam(..)) |
1244-
(&ty::TyParam(_), def::DefSelfTy(Some(_), None)) => {
1245-
// A type parameter or Self, we need to find the associated item from
1246-
// a bound.
1247-
let ty_param_node_id = ty_path_def.local_node_id();
1248-
match find_bound_for_assoc_item(this, ty_param_node_id, assoc_name, span) {
1253+
(&ty::TyParam(_), def::DefSelfTy(Some(trait_did), None)) => {
1254+
assert_eq!(trait_did.krate, ast::LOCAL_CRATE);
1255+
match find_bound_for_assoc_item(this, trait_did.node, None, assoc_name, span) {
1256+
Ok(bound) => bound,
1257+
Err(ErrorReported) => return (tcx.types.err, ty_path_def),
1258+
}
1259+
}
1260+
(&ty::TyParam(_), def::DefTyParam(_, _, param_did, param_name)) => {
1261+
assert_eq!(param_did.krate, ast::LOCAL_CRATE);
1262+
match find_bound_for_assoc_item(this,
1263+
param_did.node,
1264+
Some(param_name),
1265+
assoc_name,
1266+
span) {
12491267
Ok(bound) => bound,
12501268
Err(ErrorReported) => return (tcx.types.err, ty_path_def),
12511269
}

src/test/compile-fail/issue-26812.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn avg<T=T::Item>(_: T) {} //~ ERROR associated type `Item` not found for `T`
12+
fn main() {}

0 commit comments

Comments
 (0)