2019-07-26 21:54:25 +00:00
|
|
|
//@ run-pass
|
|
|
|
|
2018-09-17 09:18:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
2013-11-09 12:57:10 +00:00
|
|
|
|
2013-11-08 21:19:28 +00:00
|
|
|
// Test a sample usage pattern for regions. Makes use of the
|
|
|
|
// following features:
|
|
|
|
//
|
|
|
|
// - Multiple lifetime parameters
|
|
|
|
// - Arenas
|
|
|
|
|
2017-06-13 22:52:59 +00:00
|
|
|
#![feature(rustc_private, libc)]
|
2015-03-06 02:33:58 +00:00
|
|
|
|
2020-06-02 17:19:49 +00:00
|
|
|
extern crate rustc_arena;
|
2014-02-26 17:58:41 +00:00
|
|
|
extern crate libc;
|
2013-11-08 21:19:28 +00:00
|
|
|
|
2022-12-12 19:37:28 +00:00
|
|
|
// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
|
|
|
|
// files.
|
|
|
|
#[allow(unused_extern_crates)]
|
|
|
|
extern crate rustc_driver;
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
use TypeStructure::{TypeInt, TypeFunction};
|
|
|
|
use AstKind::{ExprInt, ExprVar, ExprLambda};
|
2020-06-02 17:19:49 +00:00
|
|
|
use rustc_arena::TypedArena;
|
2014-05-30 02:03:06 +00:00
|
|
|
use std::collections::HashMap;
|
2013-11-08 21:19:28 +00:00
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
type Type<'tcx> = &'tcx TypeStructure<'tcx>;
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2013-11-08 21:19:28 +00:00
|
|
|
enum TypeStructure<'tcx> {
|
|
|
|
TypeInt,
|
|
|
|
TypeFunction(Type<'tcx>, Type<'tcx>),
|
|
|
|
}
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-06 01:01:33 +00:00
|
|
|
|
2014-05-30 00:45:07 +00:00
|
|
|
impl<'tcx> PartialEq for TypeStructure<'tcx> {
|
2014-01-27 13:20:50 +00:00
|
|
|
fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
|
|
|
|
match (*self, *other) {
|
|
|
|
(TypeInt, TypeInt) => true,
|
|
|
|
(TypeFunction(s_a, s_b), TypeFunction(o_a, o_b)) => *s_a == *o_a && *s_b == *o_b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 21:19:28 +00:00
|
|
|
|
2014-05-31 17:43:52 +00:00
|
|
|
impl<'tcx> Eq for TypeStructure<'tcx> {}
|
2014-03-22 20:30:45 +00:00
|
|
|
|
2015-02-02 12:26:54 +00:00
|
|
|
type TyArena<'tcx> = TypedArena<TypeStructure<'tcx>>;
|
|
|
|
type AstArena<'ast> = TypedArena<AstStructure<'ast>>;
|
|
|
|
|
2013-11-08 21:19:28 +00:00
|
|
|
struct TypeContext<'tcx, 'ast> {
|
2015-02-02 12:26:54 +00:00
|
|
|
ty_arena: &'tcx TyArena<'tcx>,
|
2014-03-05 22:02:44 +00:00
|
|
|
types: Vec<Type<'tcx>> ,
|
2013-11-08 21:19:28 +00:00
|
|
|
type_table: HashMap<NodeId, Type<'tcx>>,
|
|
|
|
|
2015-02-02 12:26:54 +00:00
|
|
|
ast_arena: &'ast AstArena<'ast>,
|
2015-03-26 00:06:52 +00:00
|
|
|
ast_counter: usize,
|
2013-11-08 21:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx,'ast> TypeContext<'tcx, 'ast> {
|
2015-02-02 12:26:54 +00:00
|
|
|
fn new(ty_arena: &'tcx TyArena<'tcx>, ast_arena: &'ast AstArena<'ast>)
|
2013-11-08 21:19:28 +00:00
|
|
|
-> TypeContext<'tcx, 'ast> {
|
|
|
|
TypeContext { ty_arena: ty_arena,
|
2014-03-05 22:02:44 +00:00
|
|
|
types: Vec::new(),
|
2013-11-08 21:19:28 +00:00
|
|
|
type_table: HashMap::new(),
|
|
|
|
|
|
|
|
ast_arena: ast_arena,
|
|
|
|
ast_counter: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> {
|
2015-01-31 17:20:46 +00:00
|
|
|
for &ty in &self.types {
|
2013-11-08 21:19:28 +00:00
|
|
|
if *ty == s {
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 12:26:54 +00:00
|
|
|
let ty = self.ty_arena.alloc(s);
|
2013-11-08 21:19:28 +00:00
|
|
|
self.types.push(ty);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> {
|
|
|
|
self.type_table.insert(id, ty);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> {
|
|
|
|
let id = self.ast_counter;
|
|
|
|
self.ast_counter += 1;
|
2015-02-02 12:26:54 +00:00
|
|
|
self.ast_arena.alloc(AstStructure { id: NodeId {id:id}, kind: a })
|
2013-11-08 21:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2013-11-08 21:19:28 +00:00
|
|
|
struct NodeId {
|
2015-03-26 00:06:52 +00:00
|
|
|
id: usize
|
2013-11-08 21:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Ast<'ast> = &'ast AstStructure<'ast>;
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-11-08 21:19:28 +00:00
|
|
|
struct AstStructure<'ast> {
|
|
|
|
id: NodeId,
|
|
|
|
kind: AstKind<'ast>
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-11-08 21:19:28 +00:00
|
|
|
enum AstKind<'ast> {
|
|
|
|
ExprInt,
|
2015-03-26 00:06:52 +00:00
|
|
|
ExprVar(usize),
|
2013-11-08 21:19:28 +00:00
|
|
|
ExprLambda(Ast<'ast>),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
|
|
|
|
ast: Ast<'ast>) -> Type<'tcx>
|
|
|
|
{
|
|
|
|
match ast.kind {
|
|
|
|
ExprInt | ExprVar(_) => {
|
|
|
|
let ty = tcx.add_type(TypeInt);
|
|
|
|
tcx.set_type(ast.id, ty)
|
|
|
|
}
|
|
|
|
ExprLambda(ast) => {
|
|
|
|
let arg_ty = tcx.add_type(TypeInt);
|
|
|
|
let body_ty = compute_types(tcx, ast);
|
|
|
|
let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty));
|
|
|
|
tcx.set_type(ast.id, lambda_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2018-10-16 20:06:19 +00:00
|
|
|
let ty_arena = TypedArena::default();
|
|
|
|
let ast_arena = TypedArena::default();
|
2013-11-08 21:19:28 +00:00
|
|
|
let mut tcx = TypeContext::new(&ty_arena, &ast_arena);
|
|
|
|
let ast = tcx.ast(ExprInt);
|
|
|
|
let ty = compute_types(&mut tcx, ast);
|
|
|
|
assert_eq!(*ty, TypeInt);
|
|
|
|
}
|