Skip to content

Different results when expanding macros manually #6788

New issue

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

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

Already on GitHub? # to your account

Closed
auroranockert opened this issue May 28, 2013 · 5 comments
Closed

Different results when expanding macros manually #6788

auroranockert opened this issue May 28, 2013 · 5 comments
Labels
A-syntaxext Area: Syntax extensions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@auroranockert
Copy link
Contributor

The code https://gist.github.com/jensnockert/ab64d115023bf9d20261 (unfortunately requires OpenCL to link with) produces the following warnings

min.rs:21:8: 21:13 warning: value assigned to `error` is never read [-W dead-assignment (default)]
min.rs:21 let error = unsafe { $name($($arg),+) };
^~~~~
min.rs:20:0: 26:3 note: in expansion of cl_call!
min.rs:35:4: 35:93 note: expansion site
min.rs:28:0: 40:3 note: in expansion of cl_call_unknown_length!
min.rs:43:38: 43:123 note: expansion site

When compiled like rust build min.rs, but when doing the two-step process rustc --pretty expanded min.rs > min-expanded.rs then rust build min-expanded.rs it builds without warning.

@emberian
Copy link
Member

@jensnockert the gist no longer exists :(

Can you create another testcase?

@auroranockert
Copy link
Contributor Author

I'll just repost the Gist, https://gist.github.com/jensnockert/ab64d115023bf9d20261. I changed my username from JensNockert to jensnockert, and the link broke :(

@huonw
Copy link
Member

huonw commented Aug 7, 2013

Copying the unexpanded version of source code here, to avoid the link-death problems in the future:

#[link(name = "min", vers = "0.1", uuid = "430a7e67-8e6a-4041-be05-643fabff74cf")];

#[crate_type = "lib"];

#[nolink]
#[link_args = "-framework OpenCL"]
#[cfg(target_os = "macos")]
extern { }

#[nolink]
#[link_args = "-lOpenCL"]
#[cfg(target_os = "linux")]
extern { }

#[nolink]
extern {
    unsafe fn clGetPlatformInfo(platform: *libc::c_void, name: u32, value_size: libc::size_t, value: *mut libc::c_void, value_size_ret: *mut libc::size_t) -> i32;
}

macro_rules! cl_call(($name:ident : $($arg:expr),+) => ({
    let error = unsafe { $name($($arg),+) };

    if (error != 0) {
        return result::Err(error);
    }
}))

macro_rules! cl_call_unknown_length(($name:ident, $n:ty, $in:ty, $out:ty : $($arg:expr),+) => ({
    let mut n:$n = 0;

    cl_call!($name: $($arg),+, 0, ptr::mut_null(), ptr::to_mut_unsafe_ptr(&mut n));

    let mut result:~[$out] = vec::with_capacity(n as uint);

    cl_call!($name: $($arg),+, n, vec::raw::to_mut_ptr(result) as *mut $in, ptr::mut_null());

    unsafe { vec::raw::set_len(&mut result, n as uint) };

    result
}))

pub fn get_info(id: *libc::c_void, name: u32) -> result::Result<~str, i32> {
    return result::Ok(str::from_bytes(cl_call_unknown_length!(clGetPlatformInfo, libc::size_t, libc::c_void, u8: id, name)));
}

@huonw
Copy link
Member

huonw commented Nov 3, 2013

Triage: the following update seems to work (at least, it compiles for me, but I don't have opencl so it fails in linking).

#[link(name = "min", vers = "0.1", uuid = "430a7e67-8e6a-4041-be05-643fabff74cf")];
#[feature(macro_rules)];
#[crate_type = "lib"];

use std::{libc, result, ptr, vec, str};
#[nolink]
#[link_args = "-framework OpenCL"]
#[cfg(target_os = "macos")]
extern { }

#[nolink]
#[link_args = "-lOpenCL"]
#[cfg(target_os = "linux")]
extern { }

#[nolink]
extern {
    fn clGetPlatformInfo(platform: *libc::c_void, name: u32, value_size: libc::size_t, value: *mut libc::c_void, value_size_ret: *mut libc::size_t) -> i32;
}

macro_rules! cl_call(($name:ident : $($arg:expr),+) => ({
    let error = unsafe { $name($($arg),+) };

    if (error != 0) {
        return result::Err(error);
    }
}))

macro_rules! cl_call_unknown_length(
    ($name:ident, $n:ty, $in_:ty, $out:ty <- $($arg:expr),*) => ({
            let mut n:$n = 0;

            cl_call!($name: $($arg),*, 0, ptr::mut_null(), ptr::to_mut_unsafe_ptr(&mut n));

            let mut result:~[$out] = vec::with_capacity(n as uint);

            cl_call!($name: $($arg),*, n, vec::raw::to_mut_ptr(result) as *mut $in_, ptr::mut_null());

            unsafe { vec::raw::set_len(&mut result, n as uint) };

            result
}))

#[fixed_stack_segment]
pub fn get_info(id: *libc::c_void, name: u32) -> result::Result<~str, i32> {
    return result::Ok(str::from_utf8_owned(
            cl_call_unknown_length!(clGetPlatformInfo,
                                    libc::size_t,
                                    libc::c_void,
                                    u8 <- id, name)));
}

@jensnockert could you confirm? (And reopen if it's not behaving as you expect.)

@huonw huonw closed this as completed Nov 3, 2013
@emberian
Copy link
Member

emberian commented Nov 3, 2013

@huonw would it be possible to add a testcase for this?

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 25, 2021
…ip1995

move upper_case_acronyms back to style, but make the default behaviour less aggressive by default (can be unleashed via config option)

Previous discussion in the bi-weekly clippy meeting for reference: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Meeting.202021-02-23/near/227458019

Move the `upper_case_acronyms` lint back to the style group.
Only warn on fully-capitalized names by default.
Add add a clippy-config option `upper-case-acronyms-aggressive: true/false` to enabled more aggressive linting on
all substrings that could be capitalized acronyms.

---
changelog: reenable upper_case_acronyms by default but make the more aggressive linting opt-in via config option
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-syntaxext Area: Syntax extensions E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

3 participants