Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f4f4a35b
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f4f4a35b
编写于
11月 08, 2013
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new tests showing multiple lifetime parameters in use
上级
f36a891f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
165 addition
and
2 deletion
+165
-2
src/test/compile-fail/regions-variance-contravariant-use-covariant-in-second-position.rs
...ariance-contravariant-use-covariant-in-second-position.rs
+39
-0
src/test/compile-fail/regions-variance-contravariant-use-covariant.rs
...pile-fail/regions-variance-contravariant-use-covariant.rs
+2
-2
src/test/run-pass/regions-mock-tcx.rs
src/test/run-pass/regions-mock-tcx.rs
+124
-0
未找到文件。
src/test/compile-fail/regions-variance-contravariant-use-covariant-in-second-position.rs
0 → 100644
浏览文件 @
f4f4a35b
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that a type which is covariant with respect to its region
// parameter yields an error when used in a contravariant way.
//
// Note: see variance-regions-*.rs for the tests that check that the
// variance inference works in the first place.
// `S` is contravariant with respect to both parameters.
struct
S
<
'a
,
'b
>
{
f
:
&
'a
int
,
g
:
&
'b
int
,
}
fn
use_
<
'short
,
'long
>
(
c
:
S
<
'long
,
'short
>
,
s
:
&
'short
int
,
l
:
&
'long
int
,
_
where
:
Option
<&
'short
&
'long
()
>
)
{
let
_
:
S
<
'long
,
'short
>
=
c
;
// OK
let
_
:
S
<
'short
,
'short
>
=
c
;
// OK
// Test whether S<_,'short> <: S<_,'long>. Since
// 'short <= 'long, this would be true if the Contravariant type were
// covariant with respect to its parameter 'a.
let
_
:
S
<
'long
,
'long
>
=
c
;
//~ ERROR mismatched types
//~^ ERROR cannot infer an appropriate lifetime
}
fn
main
()
{}
src/test/compile-fail/regions-variance-contravariant-use-covariant.rs
浏览文件 @
f4f4a35b
...
...
@@ -14,8 +14,8 @@
// Note: see variance-regions-*.rs for the tests that check that the
// variance inference works in the first place.
// This is covariant with respect to 'a, meaning that
// Co
variant<'foo> <: Co
variant<'static> because
// This is co
ntra
variant with respect to 'a, meaning that
// Co
ntravariant<'foo> <: Contra
variant<'static> because
// 'foo <= 'static
struct
Contravariant
<
'a
>
{
f
:
&
'a
int
...
...
src/test/run-pass/regions-mock-tcx.rs
0 → 100644
浏览文件 @
f4f4a35b
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test a sample usage pattern for regions. Makes use of the
// following features:
//
// - Multiple lifetime parameters
// - Arenas
extern
mod
extra
;
use
extra
::
arena
;
use
extra
::
arena
::
Arena
;
use
std
::
hashmap
::
HashMap
;
use
std
::
cast
;
use
std
::
libc
;
use
std
::
mem
;
type
Type
<
'tcx
>
=
&
'tcx
TypeStructure
<
'tcx
>
;
#[deriving(Eq)]
enum
TypeStructure
<
'tcx
>
{
TypeInt
,
TypeFunction
(
Type
<
'tcx
>
,
Type
<
'tcx
>
),
}
struct
TypeContext
<
'tcx
,
'ast
>
{
ty_arena
:
&
'tcx
Arena
,
types
:
~
[
Type
<
'tcx
>
],
type_table
:
HashMap
<
NodeId
,
Type
<
'tcx
>>
,
ast_arena
:
&
'ast
Arena
,
ast_counter
:
uint
,
}
impl
<
'tcx
,
'ast
>
TypeContext
<
'tcx
,
'ast
>
{
fn
new
(
ty_arena
:
&
'tcx
Arena
,
ast_arena
:
&
'ast
Arena
)
->
TypeContext
<
'tcx
,
'ast
>
{
TypeContext
{
ty_arena
:
ty_arena
,
types
:
~
[],
type_table
:
HashMap
::
new
(),
ast_arena
:
ast_arena
,
ast_counter
:
0
}
}
fn
add_type
(
&
mut
self
,
s
:
TypeStructure
<
'tcx
>
)
->
Type
<
'tcx
>
{
for
&
ty
in
self
.types
.iter
()
{
if
*
ty
==
s
{
return
ty
;
}
}
let
ty
=
self
.ty_arena
.alloc
(||
s
);
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
;
self
.ast_arena
.alloc
(||
AstStructure
{
id
:
NodeId
{
id
:
id
},
kind
:
a
})
}
}
#[deriving(Eq,
IterBytes)]
struct
NodeId
{
id
:
uint
}
type
Ast
<
'ast
>
=
&
'ast
AstStructure
<
'ast
>
;
struct
AstStructure
<
'ast
>
{
id
:
NodeId
,
kind
:
AstKind
<
'ast
>
}
enum
AstKind
<
'ast
>
{
ExprInt
,
ExprVar
(
uint
),
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
()
{
let
ty_arena
=
arena
::
Arena
::
new
();
let
ast_arena
=
arena
::
Arena
::
new
();
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
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录