]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/astconv_util.rs
Auto merge of #31144 - jseyfried:remove_import_ordering_restriction, r=nrc
[rust.git] / src / librustc / middle / astconv_util.rs
1 // Copyright 2012-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 /*!
12  * This module contains a simple utility routine
13  * used by both `typeck` and `const_eval`.
14  * Almost certainly this could (and should) be refactored out of existence.
15  */
16
17 use middle::def::Def;
18 use middle::ty::{self, Ty};
19
20 use syntax::codemap::Span;
21 use rustc_front::hir as ast;
22
23 pub fn prohibit_type_params(tcx: &ty::ctxt, segments: &[ast::PathSegment]) {
24     for segment in segments {
25         for typ in segment.parameters.types() {
26             span_err!(tcx.sess, typ.span, E0109,
27                       "type parameters are not allowed on this type");
28             break;
29         }
30         for lifetime in segment.parameters.lifetimes() {
31             span_err!(tcx.sess, lifetime.span, E0110,
32                       "lifetime parameters are not allowed on this type");
33             break;
34         }
35         for binding in segment.parameters.bindings() {
36             prohibit_projection(tcx, binding.span);
37             break;
38         }
39     }
40 }
41
42 pub fn prohibit_projection(tcx: &ty::ctxt, span: Span)
43 {
44     span_err!(tcx.sess, span, E0229,
45               "associated type bindings are not allowed here");
46 }
47
48 pub fn prim_ty_to_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
49                            segments: &[ast::PathSegment],
50                            nty: ast::PrimTy)
51                            -> Ty<'tcx> {
52     prohibit_type_params(tcx, segments);
53     match nty {
54         ast::TyBool => tcx.types.bool,
55         ast::TyChar => tcx.types.char,
56         ast::TyInt(it) => tcx.mk_mach_int(it),
57         ast::TyUint(uit) => tcx.mk_mach_uint(uit),
58         ast::TyFloat(ft) => tcx.mk_mach_float(ft),
59         ast::TyStr => tcx.mk_str()
60     }
61 }
62
63 /// If a type in the AST is a primitive type, return the ty::Ty corresponding
64 /// to it.
65 pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
66                                -> Option<Ty<'tcx>> {
67     if let ast::TyPath(None, ref path) = ast_ty.node {
68         let def = match tcx.def_map.borrow().get(&ast_ty.id) {
69             None => {
70                 tcx.sess.span_bug(ast_ty.span,
71                                   &format!("unbound path {:?}", path))
72             }
73             Some(d) => d.full_def()
74         };
75         if let Def::PrimTy(nty) = def {
76             Some(prim_ty_to_ty(tcx, &path.segments, nty))
77         } else {
78             None
79         }
80     } else {
81         None
82     }
83 }