Skip to content

generic_const_expr: doc test does not compile while similar lib test compiles fine #94321

Open
@tspiteri

Description

@tspiteri
Contributor

Update(fmease, 2024-01-24): #94321 (comment):

This issue goes away if the doc tests have the #![feature(generic_const_exprs)] flag enabled.

I realized thanks to #94287 (comment); maybe a better diagnostic could help here.



The doc test for foo and the test tests::test contain the same code (the only difference is the use statement which has to be different). The lib test passes but the doc test fails to compile.

#![feature(generic_const_exprs)]

pub struct If<const CONDITION: bool>;
pub trait True {}
impl True for If<true> {}

pub struct FixedU64<const FRAC: u32>;

/// ```rust
/// use fcg5::{foo, FixedU64};
/// foo(FixedU64::<4>);
/// ```
pub fn foo<const FRAC: u32>(_fixed: FixedU64<FRAC>)
where
    If<{ FRAC <= 64 }>: True,
{
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {
        use crate::{foo, FixedU64};
        foo(FixedU64::<4>);
    }
}

cargo +nightly test:

   Compiling fcg5 v0.1.0 (/home/trevor/try/fcg5)
...
    Finished test [unoptimized + debuginfo] target(s) in 0.44s
     Running unittests src/lib.rs (target/debug/deps/fcg5-877f755fe1bd39e7)

running 1 test
test tests::test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests fcg5
...

running 1 test
test src/lib.rs - foo (line 9) ... FAILED

failures:

---- src/lib.rs - foo (line 9) stdout ----
error[E0277]: the trait bound `If<{_: bool}>: True` is not satisfied
  --> src/lib.rs:11:1
   |
5  | foo(FixedU64::<4>);
   | ^^^ the trait `True` is not implemented for `If<{_: bool}>`
   |
   = help: the following implementations were found:
             <If<true> as True>
note: required by a bound in `foo`
  --> /home/trevor/try/fcg5/src/lib.rs:15:25
   |
15 |     If<{ FRAC <= 64 }>: True,
   |                         ^^^^ required by this bound in `foo`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Couldn't compile the test.

failures:
    src/lib.rs - foo (line 9)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s

error: test failed, to rerun pass '--doc'

Meta

rustc +nightly --version --verbose:

rustc 1.61.0-nightly (532d3cda9 2022-02-23)
binary: rustc
commit-hash: 532d3cda90b8a729cd982548649d32803d265052
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Activity

tspiteri

tspiteri commented on Feb 26, 2022

@tspiteri
ContributorAuthor

This issue goes away if the doc tests have the #![feature(generic_const_exprs)] flag enabled.

I realized thanks to #94287 (comment); maybe a better diagnostic could help here.

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-diagnosticsArea: Messages for errors, warnings, and lints
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
and removed
C-bugCategory: This is a bug.
on Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-generic_const_exprs`#![feature(generic_const_exprs)]`P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ChrisDenton@fmease@tspiteri

        Issue actions

          generic_const_expr: doc test does not compile while similar lib test compiles fine · Issue #94321 · rust-lang/rust