Skip to content
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

Remove priv from the language #13547

Merged
merged 3 commits into from
Apr 16, 2014
Merged
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/liblibc/lib.rs
Original file line number Diff line number Diff line change
@@ -229,8 +229,8 @@ pub mod types {
*/
#[repr(u8)]
pub enum c_void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this become pub struct c_void { private: u8 }?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly I don't think so. I think that this was around primarily for free(malloc(size)) to get optimized away.

pub struct c_void { wut: u8 }

extern {
    fn malloc(s: uint) -> *mut c_void;
    fn free(s: *mut c_void);
}

fn main() {
    unsafe {
        let a = malloc(4);
        free(a);
    }
}
$ rustc foo.rs --emit=ir -o - -O      
; ModuleID = '-.rs'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-apple-darwin"

%struct.c_void = type { i8 }

; Function Attrs: nounwind
declare noalias %struct.c_void* @malloc(i64) unnamed_addr #0

; Function Attrs: nounwind
declare void @free(%struct.c_void* nocapture) unnamed_addr #0

; Function Attrs: nounwind uwtable
define internal void @_ZN4main20h1284b3d9a92c1d91yaa4v0.0E() unnamed_addr #1 {
entry-block:
  %0 = tail call %struct.c_void* @malloc(i64 4)
  tail call void @free(%struct.c_void* %0)
  ret void
}

define i64 @main(i64, i8**) unnamed_addr {
top:
  %2 = tail call i64 @_ZN10lang_start20he5bbdca974ae42c3eqd9v0.11.preE(i8* bitcast (void ()* @_ZN4main20h1284b3d9a92c1d91yaa4v0.0E to i8*), i64 %0, i8** %1)
  ret i64 %2
}

declare i64 @_ZN10lang_start20he5bbdca974ae42c3eqd9v0.11.preE(i8*, i64, i8**) unnamed_addr

attributes #0 = { nounwind }
attributes #1 = { nounwind uwtable }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the enum works? That seems a little peculiar?

Oh, I guess #[repr(u8)] is making the enum an actual u8, rather than just a struct with a u8 in it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the problem is

%struct.c_void = type { i8 }

which does not happen with enums because the enum is just an LLVM i8 type.

priv variant1,
priv variant2
__variant1,
__variant2,
}
pub enum FILE {}
pub enum fpos_t {}
36 changes: 23 additions & 13 deletions src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
@@ -45,10 +45,14 @@ use super::{IndependentSample, Sample, Exp};
/// for Generating Gamma Variables" *ACM Trans. Math. Softw.* 26, 3
/// (September 2000),
/// 363-372. DOI:[10.1145/358407.358414](http://doi.acm.org/10.1145/358407.358414)
pub enum Gamma {
priv Large(GammaLargeShape),
priv One(Exp),
priv Small(GammaSmallShape)
pub struct Gamma {
repr: GammaRepr,
}

enum GammaRepr {
Large(GammaLargeShape),
One(Exp),
Small(GammaSmallShape)
}

// These two helpers could be made public, but saving the
@@ -90,11 +94,12 @@ impl Gamma {
assert!(shape > 0.0, "Gamma::new called with shape <= 0");
assert!(scale > 0.0, "Gamma::new called with scale <= 0");

match shape {
let repr = match shape {
1.0 => One(Exp::new(1.0 / scale)),
0.0 .. 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
_ => Large(GammaLargeShape::new_raw(shape, scale))
}
};
Gamma { repr: repr }
}
}

@@ -131,7 +136,7 @@ impl Sample<f64> for GammaLargeShape {

impl IndependentSample<f64> for Gamma {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
match *self {
match self.repr {
Small(ref g) => g.ind_sample(rng),
One(ref g) => g.ind_sample(rng),
Large(ref g) => g.ind_sample(rng),
@@ -183,32 +188,37 @@ impl IndependentSample<f64> for GammaLargeShape {
/// let v = chi.ind_sample(&mut rand::task_rng());
/// println!("{} is from a χ²(11) distribution", v)
/// ```
pub enum ChiSquared {
pub struct ChiSquared {
repr: ChiSquaredRepr,
}

enum ChiSquaredRepr {
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
// e.g. when alpha = 1/2 as it would be for this case, so special-
// casing and using the definition of N(0,1)^2 is faster.
priv DoFExactlyOne,
priv DoFAnythingElse(Gamma)
DoFExactlyOne,
DoFAnythingElse(Gamma),
}

impl ChiSquared {
/// Create a new chi-squared distribution with degrees-of-freedom
/// `k`. Fails if `k < 0`.
pub fn new(k: f64) -> ChiSquared {
if k == 1.0 {
let repr = if k == 1.0 {
DoFExactlyOne
} else {
assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
}
};
ChiSquared { repr: repr }
}
}
impl Sample<f64> for ChiSquared {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for ChiSquared {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
match *self {
match self.repr {
DoFExactlyOne => {
// k == 1 => N(0,1)^2
let StandardNormal(norm) = rng.gen::<StandardNormal>();
30 changes: 17 additions & 13 deletions src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
@@ -206,15 +206,19 @@ impl CrateDebugContext {
}
}

pub enum FunctionDebugContext {
priv FunctionDebugContext(~FunctionDebugContextData),
priv DebugInfoDisabled,
priv FunctionWithoutDebugInfo,
pub struct FunctionDebugContext {
repr: FunctionDebugContextRepr,
}

enum FunctionDebugContextRepr {
FunctionDebugContext(~FunctionDebugContextData),
DebugInfoDisabled,
FunctionWithoutDebugInfo,
}

impl FunctionDebugContext {
fn get_ref<'a>(&'a self, cx: &CrateContext, span: Span) -> &'a FunctionDebugContextData {
match *self {
match self.repr {
FunctionDebugContext(~ref data) => data,
DebugInfoDisabled => {
cx.sess().span_bug(span, FunctionDebugContext::debuginfo_disabled_message());
@@ -544,7 +548,7 @@ pub fn create_argument_metadata(bcx: &Block, arg: &ast::Arg) {
pub fn set_source_location(fcx: &FunctionContext,
node_id: ast::NodeId,
span: Span) {
match fcx.debug_context {
match fcx.debug_context.repr {
DebugInfoDisabled => return,
FunctionWithoutDebugInfo => {
set_debug_location(fcx.ccx, UnknownLocation);
@@ -585,7 +589,7 @@ pub fn clear_source_location(fcx: &FunctionContext) {
/// and must therefore be called before the first real statement/expression of the function is
/// translated.
pub fn start_emitting_source_locations(fcx: &FunctionContext) {
match fcx.debug_context {
match fcx.debug_context.repr {
FunctionDebugContext(~ref data) => {
data.source_locations_enabled.set(true)
},
@@ -603,15 +607,15 @@ pub fn create_function_debug_context(cx: &CrateContext,
param_substs: Option<@param_substs>,
llfn: ValueRef) -> FunctionDebugContext {
if cx.sess().opts.debuginfo == NoDebugInfo {
return DebugInfoDisabled;
return FunctionDebugContext { repr: DebugInfoDisabled };
}

// Clear the debug location so we don't assign them in the function prelude. Do this here
// already, in case we do an early exit from this function.
set_debug_location(cx, UnknownLocation);

if fn_ast_id == -1 {
return FunctionWithoutDebugInfo;
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
}

let empty_generics = ast::Generics { lifetimes: Vec::new(), ty_params: OwnedSlice::empty() };
@@ -678,15 +682,15 @@ pub fn create_function_debug_context(cx: &CrateContext,
ast_map::NodeForeignItem(..) |
ast_map::NodeVariant(..) |
ast_map::NodeStructCtor(..) => {
return FunctionWithoutDebugInfo;
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
}
_ => cx.sess().bug(format!("create_function_debug_context: \
unexpected sort of node: {:?}", fnitem))
};

// This can be the case for functions inlined from another crate
if span == codemap::DUMMY_SP {
return FunctionWithoutDebugInfo;
return FunctionDebugContext { repr: FunctionWithoutDebugInfo };
}

let loc = span_start(cx, span);
@@ -761,7 +765,7 @@ pub fn create_function_debug_context(cx: &CrateContext,
fn_metadata,
&mut *fn_debug_context.scope_map.borrow_mut());

return FunctionDebugContext(fn_debug_context);
return FunctionDebugContext { repr: FunctionDebugContext(fn_debug_context) };

fn get_function_signature(cx: &CrateContext,
fn_ast_id: ast::NodeId,
@@ -2335,7 +2339,7 @@ fn DIB(cx: &CrateContext) -> DIBuilderRef {
}

fn fn_should_be_ignored(fcx: &FunctionContext) -> bool {
match fcx.debug_context {
match fcx.debug_context.repr {
FunctionDebugContext(_) => false,
_ => true
}
59 changes: 34 additions & 25 deletions src/libsyntax/util/small_vector.rs
Original file line number Diff line number Diff line change
@@ -12,15 +12,19 @@ use std::mem;
use std::vec;

/// A vector type optimized for cases where the size is almost always 0 or 1
pub enum SmallVector<T> {
priv Zero,
priv One(T),
priv Many(Vec<T> ),
pub struct SmallVector<T> {
repr: SmallVectorRepr<T>,
}

enum SmallVectorRepr<T> {
Zero,
One(T),
Many(Vec<T> ),
}

impl<T> Container for SmallVector<T> {
fn len(&self) -> uint {
match *self {
match self.repr {
Zero => 0,
One(..) => 1,
Many(ref vals) => vals.len()
@@ -30,7 +34,7 @@ impl<T> Container for SmallVector<T> {

impl<T> FromIterator<T> for SmallVector<T> {
fn from_iter<I: Iterator<T>>(iter: I) -> SmallVector<T> {
let mut v = Zero;
let mut v = SmallVector::zero();
v.extend(iter);
v
}
@@ -46,24 +50,24 @@ impl<T> Extendable<T> for SmallVector<T> {

impl<T> SmallVector<T> {
pub fn zero() -> SmallVector<T> {
Zero
SmallVector { repr: Zero }
}

pub fn one(v: T) -> SmallVector<T> {
One(v)
SmallVector { repr: One(v) }
}

pub fn many(vs: Vec<T> ) -> SmallVector<T> {
Many(vs)
pub fn many(vs: Vec<T>) -> SmallVector<T> {
SmallVector { repr: Many(vs) }
}

pub fn push(&mut self, v: T) {
match *self {
Zero => *self = One(v),
match self.repr {
Zero => self.repr = One(v),
One(..) => {
let one = mem::replace(self, Zero);
let one = mem::replace(&mut self.repr, Zero);
match one {
One(v1) => mem::replace(self, Many(vec!(v1, v))),
One(v1) => mem::replace(&mut self.repr, Many(vec!(v1, v))),
_ => unreachable!()
};
}
@@ -78,15 +82,15 @@ impl<T> SmallVector<T> {
}

pub fn get<'a>(&'a self, idx: uint) -> &'a T {
match *self {
match self.repr {
One(ref v) if idx == 0 => v,
Many(ref vs) => vs.get(idx),
_ => fail!("out of bounds access")
}
}

pub fn expect_one(self, err: &'static str) -> T {
match self {
match self.repr {
One(v) => v,
Many(v) => {
if v.len() == 1 {
@@ -100,27 +104,32 @@ impl<T> SmallVector<T> {
}

pub fn move_iter(self) -> MoveItems<T> {
match self {
let repr = match self.repr {
Zero => ZeroIterator,
One(v) => OneIterator(v),
Many(vs) => ManyIterator(vs.move_iter())
}
};
MoveItems { repr: repr }
}
}

pub enum MoveItems<T> {
priv ZeroIterator,
priv OneIterator(T),
priv ManyIterator(vec::MoveItems<T>),
pub struct MoveItems<T> {
repr: MoveItemsRepr<T>,
}

enum MoveItemsRepr<T> {
ZeroIterator,
OneIterator(T),
ManyIterator(vec::MoveItems<T>),
}

impl<T> Iterator<T> for MoveItems<T> {
fn next(&mut self) -> Option<T> {
match *self {
match self.repr {
ZeroIterator => None,
OneIterator(..) => {
let mut replacement = ZeroIterator;
mem::swap(self, &mut replacement);
mem::swap(&mut self.repr, &mut replacement);
match replacement {
OneIterator(v) => Some(v),
_ => unreachable!()
@@ -131,7 +140,7 @@ impl<T> Iterator<T> for MoveItems<T> {
}

fn size_hint(&self) -> (uint, Option<uint>) {
match *self {
match self.repr {
ZeroIterator => (0, Some(0)),
OneIterator(..) => (1, Some(1)),
ManyIterator(ref inner) => inner.size_hint()