Skip to content

Commit fc043c0

Browse files
committed
Check bounds when looking up type parameters
A mismatched type with more type parameters than the expected one causes `typeck` looking up out of the bound of type parameter vector, which leads to ICE. Closes #13466
1 parent ecc774f commit fc043c0

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/librustc/middle/typeck/check/vtable.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,17 @@ fn lookup_vtable(vcx: &VtableContext,
242242
// bounds to see if they include the trait we are looking for.
243243
let vtable_opt = match ty::get(ty).sty {
244244
ty::ty_param(param_ty {idx: n, ..}) => {
245-
let type_param_bounds: &[@ty::TraitRef] =
246-
vcx.param_env
247-
.type_param_bounds
248-
.get(n)
249-
.trait_bounds
250-
.as_slice();
251-
lookup_vtable_from_bounds(vcx, span,
252-
type_param_bounds,
253-
param_numbered(n),
254-
trait_ref)
245+
let env_bounds = &vcx.param_env.type_param_bounds;
246+
if env_bounds.len() > n {
247+
let type_param_bounds: &[@ty::TraitRef] =
248+
env_bounds.get(n).trait_bounds.as_slice();
249+
lookup_vtable_from_bounds(vcx, span,
250+
type_param_bounds,
251+
param_numbered(n),
252+
trait_ref)
253+
} else {
254+
None
255+
}
255256
}
256257

257258
ty::ty_self(_) => {

src/librustc/util/ppaux.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ pub fn ty_to_str(cx: &ctxt, typ: t) -> ~str {
393393
ty_param(param_ty {idx: id, def_id: did}) => {
394394
let ident = match cx.ty_param_defs.borrow().find(&did.node) {
395395
Some(def) => token::get_ident(def.ident).get().to_str(),
396-
// This should not happen...
397-
None => format!("BUG[{:?}]", id)
396+
// This can only happen when a type mismatch error happens and
397+
// the actual type has more type parameters than the expected one.
398+
None => format!("<generic \\#{}>", id)
398399
};
399400
if !cx.sess.verbose() {
400401
ident

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

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 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+
// Regression test for #13466
12+
13+
pub fn main() {
14+
// The expected arm type `Option<T>` has one type parameter, while
15+
// the actual arm `Result<T, E>` has two. typeck should not be
16+
// tricked into looking up a non-existing second type parameter.
17+
let _x: uint = match Some(1u) {
18+
//~^ ERROR mismatched types: expected `uint` but found `<generic #0>`
19+
Ok(u) => u, //~ ERROR mismatched types: expected `std::option::Option<uint>`
20+
Err(e) => fail!(e) //~ ERROR mismatched types: expected `std::option::Option<uint>`
21+
};
22+
}

0 commit comments

Comments
 (0)