Skip to content

Commit b3cad86

Browse files
committed
rustc: Resolve struct names in struct literals
1 parent 0d581bd commit b3cad86

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/rustc/middle/resolve3.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param};
1414
import syntax::ast::{def_upvar, def_use, def_variant, expr, expr_assign_op};
1515
import syntax::ast::{expr_binary, expr_cast, expr_field, expr_fn};
1616
import syntax::ast::{expr_fn_block, expr_index, expr_new, expr_path};
17-
import syntax::ast::{expr_unary, fn_decl, foreign_item, foreign_item_fn};
18-
import syntax::ast::{ident, trait_ref, impure_fn, instance_var, item};
19-
import syntax::ast::{item_class, item_const, item_enum, item_fn, item_mac};
20-
import syntax::ast::{item_foreign_mod, item_trait, item_impl, item_mod};
21-
import syntax::ast::{item_ty, local, local_crate, method, node_id, pat};
22-
import syntax::ast::{pat_enum, pat_ident, path, prim_ty, stmt_decl, ty,
23-
pat_box, pat_uniq, pat_lit, pat_range, pat_rec,
24-
pat_tup, pat_wild};
25-
import syntax::ast::{ty_bool, ty_char, ty_f, ty_f32, ty_f64};
17+
import syntax::ast::{expr_struct, expr_unary, fn_decl, foreign_item};
18+
import syntax::ast::{foreign_item_fn, ident, trait_ref, impure_fn};
19+
import syntax::ast::{instance_var, item, item_class, item_const, item_enum};
20+
import syntax::ast::{item_fn, item_mac, item_foreign_mod, item_impl};
21+
import syntax::ast::{item_mod, item_trait, item_ty, local, local_crate};
22+
import syntax::ast::{method, node_id, pat, pat_enum, pat_ident};
23+
import syntax::ast::{path, prim_ty, pat_box, pat_uniq, pat_lit, pat_range};
24+
import syntax::ast::{pat_rec, pat_tup, pat_wild, stmt_decl};
25+
import syntax::ast::{ty, ty_bool, ty_char, ty_f, ty_f32, ty_f64};
2626
import syntax::ast::{ty_float, ty_i, ty_i16, ty_i32, ty_i64, ty_i8, ty_int};
2727
import syntax::ast::{ty_param, ty_path, ty_str, ty_u, ty_u16, ty_u32, ty_u64};
2828
import syntax::ast::{ty_u8, ty_uint, variant, view_item, view_item_export};
@@ -604,6 +604,7 @@ class Resolver {
604604
let unused_import_lint_level: level;
605605

606606
let trait_info: hashmap<def_id,@hashmap<Atom,()>>;
607+
let structs: hashmap<def_id,()>;
607608

608609
// The number of imports that are currently unresolved.
609610
let mut unresolved_imports: uint;
@@ -657,6 +658,7 @@ class Resolver {
657658
self.unused_import_lint_level = unused_import_lint_level(session);
658659

659660
self.trait_info = new_def_hash();
661+
self.structs = new_def_hash();
660662

661663
self.unresolved_imports = 0u;
662664

@@ -915,6 +917,9 @@ class Resolver {
915917

916918
(*name_bindings).define_impl(impl_info);
917919

920+
// Record the def ID of this struct.
921+
self.structs.insert(local_def(item.id), ());
922+
918923
visit_item(item, new_parent, visitor);
919924
}
920925

@@ -4162,6 +4167,41 @@ class Resolver {
41624167
visitor);
41634168
}
41644169

4170+
expr_struct(path, _) {
4171+
// Resolve the path to the structure it goes to.
4172+
//
4173+
// XXX: We might want to support explicit type parameters in
4174+
// the path, in which case this gets a little more
4175+
// complicated:
4176+
//
4177+
// 1. Should we go through the ast_path_to_ty() path, which
4178+
// handles typedefs and the like?
4179+
//
4180+
// 2. If so, should programmers be able to write this?
4181+
//
4182+
// class Foo<A> { ... }
4183+
// type Bar<A> = Foo<A>;
4184+
// let bar = Bar { ... } // no type parameters
4185+
4186+
alt self.resolve_path(path, TypeNS, false, visitor) {
4187+
some(definition @ def_ty(class_id))
4188+
if self.structs.contains_key(class_id) {
4189+
4190+
self.record_def(expr.id, def_class(class_id));
4191+
}
4192+
_ {
4193+
self.session.span_err(path.span,
4194+
#fmt("`%s` does not name a \
4195+
structure",
4196+
connect(path.idents.map
4197+
(|x| *x),
4198+
~"::")));
4199+
}
4200+
}
4201+
4202+
visit_expr(expr, (), visitor);
4203+
}
4204+
41654205
_ {
41664206
visit_expr(expr, (), visitor);
41674207
}

0 commit comments

Comments
 (0)