Skip to content

Commit 335121d

Browse files
committed
Rename get_type to get_struct_type and return StructType
as it can only ever return a StructType
1 parent 0dc6e72 commit 335121d

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

src/module.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::execution_engine::ExecutionEngine;
3838
use crate::memory_buffer::MemoryBuffer;
3939
use crate::support::LLVMString;
4040
use crate::targets::{InitializationConfig, Target, TargetTriple};
41-
use crate::types::{AsTypeRef, BasicType, FunctionType, BasicTypeEnum};
41+
use crate::types::{AsTypeRef, BasicType, FunctionType, StructType};
4242
use crate::values::{AsValueRef, FunctionValue, GlobalValue, MetadataValue};
4343
#[llvm_versions(7.0..=latest)]
4444
use crate::values::BasicValue;
@@ -291,7 +291,7 @@ impl<'ctx> Module<'ctx> {
291291
}
292292

293293

294-
/// Gets a `BasicTypeEnum` of a named type in a `Module`.
294+
/// Gets a named `StructType` from this `Module`'s `Context`.
295295
///
296296
/// # Example
297297
///
@@ -301,24 +301,24 @@ impl<'ctx> Module<'ctx> {
301301
/// let context = Context::create();
302302
/// let module = context.create_module("my_module");
303303
///
304-
/// assert!(module.get_type("foo").is_none());
304+
/// assert!(module.get_struct_type("foo").is_none());
305305
///
306306
/// let opaque = context.opaque_struct_type("foo");
307307
///
308-
/// assert_eq!(module.get_type("foo").unwrap(), opaque.into());
308+
/// assert_eq!(module.get_struct_type("foo").unwrap(), opaque);
309309
/// ```
310-
pub fn get_type(&self, name: &str) -> Option<BasicTypeEnum<'ctx>> {
310+
pub fn get_struct_type(&self, name: &str) -> Option<StructType<'ctx>> {
311311
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
312312

313-
let type_ = unsafe {
313+
let struct_type = unsafe {
314314
LLVMGetTypeByName(self.module.get(), c_string.as_ptr())
315315
};
316316

317-
if type_.is_null() {
317+
if struct_type.is_null() {
318318
return None;
319319
}
320320

321-
Some(BasicTypeEnum::new(type_))
321+
Some(StructType::new(struct_type))
322322
}
323323

324324
/// Assigns a `TargetTriple` to this `Module`.

tests/all/test_context.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
extern crate inkwell;
2-
3-
use self::inkwell::AddressSpace;
4-
use self::inkwell::context::Context;
1+
use inkwell::AddressSpace;
2+
use inkwell::context::Context;
53

64
#[test]
75
fn test_no_context_double_free() {
@@ -13,21 +11,6 @@ fn test_no_context_double_free() {
1311
}
1412
}
1513

16-
// FIXME: This isn't actually safe and stopped working as
17-
// of a recent rust version (late 2018)
18-
// #[test]
19-
// fn test_no_context_double_free2() {
20-
// let context = Context::create();
21-
// let int = context.i8_type();
22-
// let context2 = int.get_context();
23-
24-
// fn move_(_: Context) {}
25-
26-
// move_(context);
27-
28-
// context2.i8_type().const_int(0, false);
29-
// }
30-
3114
#[test]
3215
fn test_no_context_double_free3() {
3316
unsafe {
@@ -58,14 +41,11 @@ fn test_basic_block_context() {
5841
let void_type = context.void_type();
5942
let fn_type = void_type.fn_type(&[], false);
6043
let fn_value = module.add_function("my_fn", fn_type, None);
61-
// REVIEW: Should get rid of this if it uses global ctx?
6244
let basic_block = context.append_basic_block(fn_value, "entry");
6345

6446
assert_eq!(*basic_block.get_context(), context);
6547
}
6648

67-
// REVIEW: Is it bad that StructType, which uses global ctx, uses types of
68-
// a local context?
6949
#[test]
7050
fn test_values_get_context() {
7151
let context = Context::create();

tests/all/test_module.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,30 @@ fn test_garbage_ir_fails_create_module_from_ir_copy() {
146146
}
147147

148148
#[test]
149-
fn test_get_type() {
149+
fn test_get_struct_type() {
150150
let context = Context::create();
151151
let module = context.create_module("my_module");
152152

153153
assert_eq!(*module.get_context(), context);
154-
assert!(module.get_type("foo").is_none());
154+
assert!(module.get_struct_type("foo").is_none());
155155

156156
let opaque = context.opaque_struct_type("foo");
157157

158-
assert_eq!(module.get_type("foo").unwrap().into_struct_type(), opaque);
158+
assert_eq!(module.get_struct_type("foo").unwrap(), opaque);
159159
}
160160

161161
#[test]
162-
fn test_get_type_global_context() {
162+
fn test_get_struct_type_global_context() {
163163
unsafe {
164164
Context::get_global(|context| {
165165
let module = context.create_module("my_module");
166166

167167
assert_eq!(*module.get_context(), *context);
168-
assert!(module.get_type("foo").is_none());
168+
assert!(module.get_struct_type("foo").is_none());
169169

170170
let opaque = context.opaque_struct_type("foo");
171171

172-
assert_eq!(module.get_type("foo").unwrap().into_struct_type(), opaque);
172+
assert_eq!(module.get_struct_type("foo").unwrap(), opaque);
173173
})
174174
}
175175
}

0 commit comments

Comments
 (0)