Skip to content

librustc: Implement &static as the replacement for Durable #4409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/libcore/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
use cmp;
use num;

pub use cmath::c_float_utils::*;
pub use cmath::c_float_utils::{acos, asin, atan, atan2, cbrt, ceil};
pub use cmath::c_float_utils::{copysign, cos, cosh, erf, erfc, exp, expm1};
pub use cmath::c_float_utils::{exp2, abs, abs_sub, mul_add, fmax, fmin};
pub use cmath::c_float_utils::{nextafter, frexp, hypot, ldexp, lgamma};
pub use cmath::c_float_utils::{ln, log_radix, ln1p, log10, log2, ilog_radix};
pub use cmath::c_float_utils::{modf, pow, round, sin, sinh, sqrt, tan};
pub use cmath::c_float_utils::{tanh, tgamma, trunc};
pub use cmath::c_float_targ_consts::*;

// These are not defined inside consts:: for consistency with
Expand Down Expand Up @@ -53,6 +59,10 @@ pub pure fn ge(x: f32, y: f32) -> bool { return x >= y; }

pub pure fn gt(x: f32, y: f32) -> bool { return x > y; }

/// Returns `x` rounded down
#[inline(always)]
pub pure fn floor(x: f32) -> f32 { unsafe { floorf32(x) } }

// FIXME (#1999): replace the predicates below with llvm intrinsics or
// calls to the libmath macros in the rust runtime for performance.

Expand Down Expand Up @@ -185,6 +195,11 @@ impl f32: num::One {
static pure fn one() -> f32 { 1.0 }
}

#[abi="rust-intrinsic"]
pub extern {
fn floorf32(val: f32) -> f32;
}

//
// Local Variables:
// mode: rust
Expand Down
19 changes: 17 additions & 2 deletions src/libcore/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ use cmp;
use libc;
use num;

pub use cmath::c_double_utils::*;
pub use cmath::c_double_utils::{acos, asin, atan, atan2, cbrt, ceil};
pub use cmath::c_double_utils::{copysign, cos, cosh, erf, erfc, exp, expm1};
pub use cmath::c_double_utils::{exp2, abs, abs_sub, mul_add, fmax, fmin};
pub use cmath::c_double_utils::{nextafter, frexp, hypot, ldexp, lgamma};
pub use cmath::c_double_utils::{ln, log_radix, ln1p, log10, log2, ilog_radix};
pub use cmath::c_double_utils::{modf, pow, round, sin, sinh, sqrt, tan};
pub use cmath::c_double_utils::{tanh, tgamma, trunc, j0, j1, jn, y0, y1, yn};
pub use cmath::c_double_targ_consts::*;

// FIXME (#1433): obtain these in a different way
Expand Down Expand Up @@ -113,11 +119,15 @@ pub pure fn is_infinite(x: f64) -> bool {
return x == infinity || x == neg_infinity;
}

/// Returns true if `x`is a finite number
/// Returns true if `x` is a finite number
pub pure fn is_finite(x: f64) -> bool {
return !(is_NaN(x) || is_infinite(x));
}

/// Returns `x` rounded down
#[inline(always)]
pub pure fn floor(x: f64) -> f64 { unsafe { floorf64(x) } }

// FIXME (#1999): add is_normal, is_subnormal, and fpclassify

/* Module: consts */
Expand Down Expand Up @@ -206,6 +216,11 @@ impl f64: num::One {
static pure fn one() -> f64 { 1.0 }
}

#[abi="rust-intrinsic"]
pub extern {
fn floorf64(val: f64) -> f64;
}

//
// Local Variables:
// mode: rust
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn kind_to_str(k: Kind) -> ~str {
if ty::kind_can_be_sent(k) {
kinds.push(~"owned");
} else if ty::kind_is_durable(k) {
kinds.push(~"durable");
kinds.push(~"&static");
}

str::connect(kinds, ~" ")
Expand Down Expand Up @@ -571,7 +571,7 @@ fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
match ty::get(ty).sty {
ty::ty_param(*) => {
tcx.sess.span_err(sp, ~"value may contain borrowed \
pointers; use `durable` bound");
pointers; use `&static` bound");
}
_ => {
tcx.sess.span_err(sp, ~"value may contain borrowed \
Expand Down
17 changes: 13 additions & 4 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use middle::pat_util::{pat_bindings};
use core::cmp;
use core::str;
use core::vec;
use syntax::ast::{_mod, add, arm, binding_mode, bitand, bitor, bitxor, blk};
use syntax::ast::{capture_clause};
use syntax::ast::{RegionTyParamBound, TraitTyParamBound, _mod, add, arm};
use syntax::ast::{binding_mode, bitand, bitor, bitxor, blk, capture_clause};
use syntax::ast::{crate, crate_num, decl_item, def, def_arg, def_binding};
use syntax::ast::{def_const, def_foreign_mod, def_fn, def_id, def_label};
use syntax::ast::{def_local, def_mod, def_prim_ty, def_region, def_self};
Expand Down Expand Up @@ -850,6 +850,8 @@ fn Resolver(session: Session, lang_items: LanguageItems,
current_trait_refs: None,

self_ident: special_idents::self_,
type_self_ident: special_idents::type_self,

primitive_type_table: @PrimitiveTypeTable(session.
parse_sess.interner),

Expand Down Expand Up @@ -905,6 +907,8 @@ struct Resolver {

// The ident for the keyword "self".
self_ident: ident,
// The ident for the non-keyword "Self".
type_self_ident: ident,

// The idents for the primitive types.
primitive_type_table: @PrimitiveTypeTable,
Expand Down Expand Up @@ -3803,6 +3807,8 @@ impl Resolver {
(*self.type_ribs).push(self_type_rib);
self_type_rib.bindings.insert(self.self_ident,
dl_def(def_self_ty(item.id)));
self_type_rib.bindings.insert(self.type_self_ident,
dl_def(def_self_ty(item.id)));

// Create a new rib for the trait-wide type parameters.
do self.with_type_parameter_rib
Expand Down Expand Up @@ -4111,8 +4117,11 @@ impl Resolver {
fn resolve_type_parameters(type_parameters: ~[ty_param],
visitor: ResolveVisitor) {
for type_parameters.each |type_parameter| {
for type_parameter.bounds.each |bound| {
self.resolve_type(**bound, visitor);
for type_parameter.bounds.each |&bound| {
match bound {
TraitTyParamBound(ty) => self.resolve_type(ty, visitor),
RegionTyParamBound => {}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ fn substs_to_str(cx: ctxt, substs: &substs) -> ~str {
fn param_bound_to_str(cx: ctxt, pb: &param_bound) -> ~str {
match *pb {
bound_copy => ~"copy",
bound_durable => ~"durable",
bound_durable => ~"&static",
bound_owned => ~"owned",
bound_const => ~"const",
bound_trait(t) => ::util::ppaux::ty_to_str(cx, t)
Expand Down
59 changes: 33 additions & 26 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use util::ppaux::bound_to_str;
use core::dvec;
use core::option;
use core::vec;
use syntax::ast::{RegionTyParamBound, TraitTyParamBound};
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::{local_def, split_trait_methods};
Expand Down Expand Up @@ -906,36 +907,42 @@ fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item)
}
}

// Translate the AST's notion of ty param bounds (which are just newtyped Tys)
// to ty's notion of ty param bounds, which can either be user-defined traits,
// or one of the four built-in traits (formerly known as kinds): Const, Copy,
// Durable, and Send.
// Translate the AST's notion of ty param bounds (which are an enum consisting
// of a newtyped Ty or a region) to ty's notion of ty param bounds, which can
// either be user-defined traits, or one of the four built-in traits (formerly
// known as kinds): Const, Copy, Durable, and Send.
fn compute_bounds(ccx: @crate_ctxt,
ast_bounds: @~[ast::ty_param_bound]) -> ty::param_bounds {
ast_bounds: @~[ast::ty_param_bound])
-> ty::param_bounds {
@do vec::flat_map(*ast_bounds) |b| {
let li = &ccx.tcx.lang_items;
let ity = ast_ty_to_ty(ccx, empty_rscope, **b);
match ty::get(ity).sty {
ty::ty_trait(did, _, _) => {
if did == li.owned_trait() {
~[ty::bound_owned]
} else if did == li.copy_trait() {
~[ty::bound_copy]
} else if did == li.const_trait() {
~[ty::bound_const]
} else if did == li.durable_trait() {
~[ty::bound_durable]
} else {
// Must be a user-defined trait
~[ty::bound_trait(ity)]
match b {
&TraitTyParamBound(b) => {
let li = &ccx.tcx.lang_items;
let ity = ast_ty_to_ty(ccx, empty_rscope, b);
match ty::get(ity).sty {
ty::ty_trait(did, _, _) => {
if did == li.owned_trait() {
~[ty::bound_owned]
} else if did == li.copy_trait() {
~[ty::bound_copy]
} else if did == li.const_trait() {
~[ty::bound_const]
} else if did == li.durable_trait() {
~[ty::bound_durable]
} else {
// Must be a user-defined trait
~[ty::bound_trait(ity)]
}
}
_ => {
ccx.tcx.sess.span_err(
(*b).span, ~"type parameter bounds must be \
trait types");
~[]
}
}
}
_ => {
ccx.tcx.sess.span_err(
(*b).span, ~"type parameter bounds must be \
trait types");
~[]
}
&RegionTyParamBound => ~[ty::bound_durable]
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ const crate_node_id: node_id = 0;
// typeck::collect::compute_bounds matches these against
// the "special" built-in traits (see middle::lang_items) and
// detects Copy, Send, Owned, and Const.
enum ty_param_bound = @Ty;
enum ty_param_bound {
TraitTyParamBound(@Ty),
RegionTyParamBound
}

#[auto_encode]
#[auto_decode]
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/auto_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ priv impl ext_ctxt {
path: @ast::path,
bounds: @~[ast::ty_param_bound]
) -> ast::ty_param {
let bound = ast::ty_param_bound(@{
let bound = ast::TraitTyParamBound(@{
id: self.next_id(),
node: ast::ty_path(path, self.next_id()),
span: span,
Expand Down Expand Up @@ -397,7 +397,7 @@ fn mk_impl(
let mut trait_tps = vec::append(
~[ty_param],
do tps.map |tp| {
let t_bound = ast::ty_param_bound(@{
let t_bound = ast::TraitTyParamBound(@{
id: cx.next_id(),
node: ast::ty_path(path, cx.next_id()),
span: span,
Expand Down
16 changes: 8 additions & 8 deletions src/libsyntax/ext/deriving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

use core::prelude::*;

use ast::{Ty, and, bind_by_ref, binop, deref, enum_def, enum_variant_kind};
use ast::{expr, expr_match, ident, item, item_, item_struct, item_enum};
use ast::{item_impl, m_imm, meta_item, method, named_field, or, pat};
use ast::{pat_ident, pat_wild, public, pure_fn, re_anon, stmt, struct_def};
use ast::{struct_variant_kind, sty_by_ref, sty_region, tuple_variant_kind};
use ast::{ty_nil, ty_param, ty_param_bound, ty_path, ty_rptr, unnamed_field};
use ast::{variant};
use ast::{TraitTyParamBound, Ty, and, bind_by_ref, binop, deref, enum_def};
use ast::{enum_variant_kind, expr, expr_match, ident, item, item_, item_enum};
use ast::{item_impl, item_struct, m_imm, meta_item, method, named_field, or};
use ast::{pat, pat_ident, pat_wild, public, pure_fn, re_anon, stmt};
use ast::{struct_def, struct_variant_kind, sty_by_ref, sty_region};
use ast::{tuple_variant_kind, ty_nil, ty_param, ty_path, ty_rptr};
use ast::{unnamed_field, variant};
use ext::base::ext_ctxt;
use ext::build;
use codemap::span;
Expand Down Expand Up @@ -211,7 +211,7 @@ fn create_derived_impl(cx: ext_ctxt,
let bound = build::mk_ty_path_global(cx,
span,
trait_path.map(|x| *x));
let bounds = @~[ ty_param_bound(bound) ];
let bounds = @~[ TraitTyParamBound(bound) ];
let impl_ty_param = build::mk_ty_param(cx, ty_param.ident, bounds);
impl_ty_params.push(move impl_ty_param);
}
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ fn fold_fn_decl(decl: ast::fn_decl, fld: ast_fold) -> ast::fn_decl {
}

fn fold_ty_param_bound(tpb: ty_param_bound, fld: ast_fold) -> ty_param_bound {
ty_param_bound(fld.fold_ty(*tpb))
match tpb {
TraitTyParamBound(ty) => TraitTyParamBound(fld.fold_ty(ty)),
RegionTyParamBound => RegionTyParamBound
}
}

fn fold_ty_param(tp: ty_param, fld: ast_fold) -> ty_param {
Expand Down
22 changes: 16 additions & 6 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

use core::prelude::*;

use ast::{ProtoBox, ProtoUniq, provided, public, pure_fn, purity, re_static};
use ast::{ProtoBox, ProtoUniq, RegionTyParamBound, TraitTyParamBound};
use ast::{provided, public, pure_fn, purity, re_static};
use ast::{_mod, add, arg, arm, attribute, bind_by_ref, bind_infer};
use ast::{bind_by_value, bind_by_move, bitand, bitor, bitxor, blk};
use ast::{blk_check_mode, box, by_copy, by_move, by_ref, by_val};
Expand Down Expand Up @@ -2399,8 +2400,16 @@ impl Parser {
fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] {
let mut bounds = ~[];
if self.eat(token::COLON) {
while is_ident(self.token) {
if is_ident(self.token) {
loop {
if self.eat(token::BINOP(token::AND)) {
if self.eat_keyword(~"static") {
bounds.push(RegionTyParamBound);
} else {
self.span_err(copy self.span,
~"`&static` is the only permissible \
region bound here");
}
} else if is_ident(self.token) {
let maybe_bound = match self.token {
token::IDENT(copy sid, _) => {
match *self.id_to_str(sid) {
Expand All @@ -2413,7 +2422,7 @@ impl Parser {
ObsoleteLowerCaseKindBounds);
// Bogus value, but doesn't matter, since
// is an error
Some(ty_param_bound(self.mk_ty_path(sid)))
Some(TraitTyParamBound(self.mk_ty_path(sid)))
}

_ => None
Expand All @@ -2428,11 +2437,12 @@ impl Parser {
bounds.push(bound);
}
None => {
bounds.push(ty_param_bound(self.parse_ty(false)));
let ty = self.parse_ty(false);
bounds.push(TraitTyParamBound(ty));
}
}
} else {
bounds.push(ty_param_bound(self.parse_ty(false)));
break;
}
}
}
Expand Down
Loading