Skip to content

Commit e4e7aa2

Browse files
committed
Auto merge of #21744 - eddyb:rvalue-promotion, r=nikomatsakis
This includes everything necessary for promoting borrows of constant rvalues to `'static`. That is, `&expr` will have the type `&'static T` if `const T: &'static T = &expr;` is valid. There is a small exception, dereferences of raw pointers, as they misbehave. They still "work" in constants as I didn't want to break legitimate uses (are there any?). The qualification done here can be expanded to allow simple CTFE via `const fn`.
2 parents c5db290 + b49f528 commit e4e7aa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1898
-1648
lines changed

src/librand/reseeding.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ pub trait Reseeder<R> {
134134
/// Reseed an RNG using a `Default` instance. This reseeds by
135135
/// replacing the RNG with the result of a `Default::default` call.
136136
#[derive(Copy)]
137-
pub struct ReseedWithDefault;
137+
pub struct ReseedWithDefault { __hack: [u8; 0] }
138+
// FIXME(#21721) used to be an unit struct but that can cause
139+
// certain LLVM versions to abort during optimizations.
140+
#[allow(non_upper_case_globals)]
141+
pub const ReseedWithDefault: ReseedWithDefault = ReseedWithDefault { __hack: [] };
138142

139143
impl<R: Rng + Default> Reseeder<R> for ReseedWithDefault {
140144
fn reseed(&mut self, rng: &mut R) {

src/librustc/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ register_diagnostics! {
5959
E0010,
6060
E0011,
6161
E0012,
62+
E0013,
6263
E0014,
6364
E0015,
6465
E0016,

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ pub mod middle {
9090
pub mod check_loop;
9191
pub mod check_match;
9292
pub mod check_rvalues;
93-
pub mod check_static;
9493
pub mod const_eval;
9594
pub mod dataflow;
9695
pub mod dead;

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl LintPass for TypeLimits {
199199
if let ast::LitInt(shift, _) = lit.node { shift >= bits }
200200
else { false }
201201
} else {
202-
match eval_const_expr_partial(cx.tcx, &**r) {
202+
match eval_const_expr_partial(cx.tcx, &**r, Some(cx.tcx.types.uint)) {
203203
Ok(const_int(shift)) => { shift as u64 >= bits },
204204
Ok(const_uint(shift)) => { shift >= bits },
205205
_ => { false }

src/librustc/metadata/common.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
pub use self::astencode_tag::*;
1414

15-
use std::mem;
1615
use back::svh::Svh;
1716

1817
// EBML enum definitions and utils shared by the encoder and decoder
@@ -113,7 +112,7 @@ pub const tag_items_data_item_reexport_def_id: uint = 0x39;
113112
pub const tag_items_data_item_reexport_name: uint = 0x3a;
114113

115114
// used to encode crate_ctxt side tables
116-
#[derive(Copy, PartialEq)]
115+
#[derive(Copy, PartialEq, FromPrimitive)]
117116
#[repr(uint)]
118117
pub enum astencode_tag { // Reserves 0x40 -- 0x5f
119118
tag_ast = 0x40,
@@ -144,17 +143,7 @@ pub enum astencode_tag { // Reserves 0x40 -- 0x5f
144143
tag_table_upvar_capture_map = 0x56,
145144
tag_table_capture_modes = 0x57,
146145
tag_table_object_cast_map = 0x58,
147-
}
148-
149-
static first_astencode_tag: uint = tag_ast as uint;
150-
static last_astencode_tag: uint = tag_table_object_cast_map as uint;
151-
impl astencode_tag {
152-
pub fn from_uint(value : uint) -> Option<astencode_tag> {
153-
let is_a_tag = first_astencode_tag <= value && value <= last_astencode_tag;
154-
if !is_a_tag { None } else {
155-
Some(unsafe { mem::transmute::<uint, astencode_tag>(value) })
156-
}
157-
}
146+
tag_table_const_qualif = 0x59,
158147
}
159148

160149
pub const tag_item_trait_item_sort: uint = 0x60;

src/librustc/middle/astencode.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use metadata::tydecode;
2323
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter};
2424
use metadata::tydecode::{RegionParameter, ClosureSource};
2525
use metadata::tyencode;
26+
use middle::check_const::ConstQualif;
2627
use middle::mem_categorization::Typer;
2728
use middle::subst;
2829
use middle::subst::VecPerParamSpace;
@@ -38,6 +39,7 @@ use syntax::ptr::P;
3839
use syntax;
3940

4041
use std::old_io::Seek;
42+
use std::num::FromPrimitive;
4143
use std::rc::Rc;
4244

4345
use rbml::io::SeekableMemWriter;
@@ -1305,6 +1307,15 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
13051307
})
13061308
})
13071309
}
1310+
1311+
for &qualif in tcx.const_qualif_map.borrow().get(&id).iter() {
1312+
rbml_w.tag(c::tag_table_const_qualif, |rbml_w| {
1313+
rbml_w.id(id);
1314+
rbml_w.tag(c::tag_table_val, |rbml_w| {
1315+
qualif.encode(rbml_w).unwrap()
1316+
})
1317+
})
1318+
}
13081319
}
13091320

13101321
trait doc_decoder_helpers {
@@ -1836,8 +1847,8 @@ fn decode_side_tables(dcx: &DecodeContext,
18361847
debug!(">> Side table document with tag 0x{:x} \
18371848
found for id {} (orig {})",
18381849
tag, id, id0);
1839-
1840-
match c::astencode_tag::from_uint(tag) {
1850+
let decoded_tag: Option<c::astencode_tag> = FromPrimitive::from_uint(tag);
1851+
match decoded_tag {
18411852
None => {
18421853
dcx.tcx.sess.bug(
18431854
&format!("unknown tag found in side tables: {:x}",
@@ -1919,6 +1930,10 @@ fn decode_side_tables(dcx: &DecodeContext,
19191930
dcx.tcx.closure_kinds.borrow_mut().insert(ast_util::local_def(id),
19201931
closure_kind);
19211932
}
1933+
c::tag_table_const_qualif => {
1934+
let qualif: ConstQualif = Decodable::decode(val_dsr).unwrap();
1935+
dcx.tcx.const_qualif_map.borrow_mut().insert(id, qualif);
1936+
}
19221937
_ => {
19231938
dcx.tcx.sess.bug(
19241939
&format!("unknown tag found in side tables: {:x}",

0 commit comments

Comments
 (0)