public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope
@ 2021-02-23 22:39 patrick.kox at commandoregel dot be
  2023-08-15 19:00 ` [Bug c++/99232] " yagreg7 at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: patrick.kox at commandoregel dot be @ 2021-02-23 22:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99232

            Bug ID: 99232
           Summary: Exported variable in module gives error: 'lambda' was
                    not declared in this scope
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick.kox at commandoregel dot be
  Target Milestone: ---

When compiling Ex11-01 from the book "Beginning C++20" by Horton and Van Weert
there is an error message that the exported variable (const double) was not
declared.

here is the code:

//math.cpp
export module math;

export auto square(const auto& x) { return x * x; }  // An abbreviated function
template

export const double lambda{1.303577269034296391257};        // Conway's
constant

export enum class Oddity { Even, Odd };
bool isOdd(int x) { return x % 2 != 0; }  // Module-local function (not
exported)
export auto getOddity(int x) { return isOdd(x) ? Oddity::Odd : Oddity::Even; }

//-------------------------------

// main.cpp
// Consuming your own module
import <iostream>;
#define FMT_HEADER_ONLY
#include "fmt/format.h"

import math;

int main()
{
  std::cout << "Lambda squared: " << square(lambda) << std::endl;

  int number;
  std::cout << "\nPlease enter an odd number: ";
  std::cin >> number;
  std::cout << std::endl;

  switch (getOddity(number))
  {
    using enum Oddity;
  case Odd:
    std::cout << "Well done! And remember: you have to be odd to be number
one!";
    break;
  case Even:
    fmt::print("Odd, {} seems to be even?", number);
    break;
  }
  std::cout << std::endl;
}
//-------------------------------

To compile I do the following:
1. copy iostream into my source directory
2. execute the command: g++ -Wall -Wextra -fmodules-ts -std=c++20 -c -x
c++-system-header iostream
3. execute the command: g++ -fmodules-ts -Wall -Wextra -std=c++20 -pedantic
-pthread -lfmt math.cpp main.cpp

//-------------------------------
The full error message is:
main.cpp: In function ‘int main()’:
main.cpp:13:45: error: ‘lambda’ was not declared in this scope
   13 |   std::cout << "Lambda squared: " << square(lambda) << std::endl;
      |   


//-------------------------------
If I move the declaration of the lambda variable to the main.cpp sourcefile the
code does compile, so the other exports seem to work correctly.

the version of GCC is: g++ (GCC) 11.0.0 20210214 (experimental)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99232] Exported variable in module gives error: 'lambda' was not declared in this scope
  2021-02-23 22:39 [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope patrick.kox at commandoregel dot be
@ 2023-08-15 19:00 ` yagreg7 at gmail dot com
  2023-11-24 13:55 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: yagreg7 at gmail dot com @ 2023-08-15 19:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99232

Gregory Dushkin <yagreg7 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yagreg7 at gmail dot com

--- Comment #1 from Gregory Dushkin <yagreg7 at gmail dot com> ---
The issue is still present in GCC 13.2.1 and appears to only affect the actual
variables but not references to them.
E.g. the code:
```
// test.cc
export module test;
export constexpr int a = 42;
// main.cc
import test;
int main() { return a; }
```
would give:
```
$ g++ test.cc -std=c++20 -fmodules-ts -c
$ g++ main.cc -std=c++20 -fmodules-ts -c
main.cc: In function ‘int main()’:
main.cc:4:12: error: ‘a’ was not declared in this scope
    4 |     return a;
      |
```

but

```
// test.cc
export module test;
export constexpr int a_ = 42;
export constexpr const int& a = a_;
// main.cc
import test;
int main() { return a; }
```
compiles successfully using the same commands.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99232] Exported variable in module gives error: 'lambda' was not declared in this scope
  2021-02-23 22:39 [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope patrick.kox at commandoregel dot be
  2023-08-15 19:00 ` [Bug c++/99232] " yagreg7 at gmail dot com
@ 2023-11-24 13:55 ` cvs-commit at gcc dot gnu.org
  2024-02-11 13:42 ` nshead at gcc dot gnu.org
  2024-02-11 14:07 ` nshead at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-24 13:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99232

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Nathaniel Shead <nshead@gcc.gnu.org>:

https://gcc.gnu.org/g:726723c476800285cfbdfce612cedde4a9a7ad58

commit r14-5826-g726723c476800285cfbdfce612cedde4a9a7ad58
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Wed Nov 15 20:50:53 2023 +1100

    c++: Allow exporting const-qualified namespace-scope variables [PR99232]

    By [basic.link] p3.2.1, a non-template non-volatile const-qualified
    variable is not necessarily internal linkage in a module declaration,
    and rather may have module linkage (or external linkage if it is
    exported, see p4.8).

            PR c++/99232

    gcc/cp/ChangeLog:

            * decl.cc (grokvardecl): Don't mark variables attached to
            modules as internal.

    gcc/testsuite/ChangeLog:

            * g++.dg/modules/pr99232_a.C: New test.
            * g++.dg/modules/pr99232_b.C: New test.

    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99232] Exported variable in module gives error: 'lambda' was not declared in this scope
  2021-02-23 22:39 [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope patrick.kox at commandoregel dot be
  2023-08-15 19:00 ` [Bug c++/99232] " yagreg7 at gmail dot com
  2023-11-24 13:55 ` cvs-commit at gcc dot gnu.org
@ 2024-02-11 13:42 ` nshead at gcc dot gnu.org
  2024-02-11 14:07 ` nshead at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: nshead at gcc dot gnu.org @ 2024-02-11 13:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99232

Nathaniel Shead <nshead at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
           Assignee|unassigned at gcc dot gnu.org      |nshead at gcc dot gnu.org
                 CC|                            |nshead at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED
   Target Milestone|---                         |14.0

--- Comment #3 from Nathaniel Shead <nshead at gcc dot gnu.org> ---
Fixed for GCC 14.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/99232] Exported variable in module gives error: 'lambda' was not declared in this scope
  2021-02-23 22:39 [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope patrick.kox at commandoregel dot be
                   ` (2 preceding siblings ...)
  2024-02-11 13:42 ` nshead at gcc dot gnu.org
@ 2024-02-11 14:07 ` nshead at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: nshead at gcc dot gnu.org @ 2024-02-11 14:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99232

Nathaniel Shead <nshead at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |e9leyland at outlook dot com

--- Comment #4 from Nathaniel Shead <nshead at gcc dot gnu.org> ---
*** Bug 102545 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-11 14:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-23 22:39 [Bug c++/99232] New: Exported variable in module gives error: 'lambda' was not declared in this scope patrick.kox at commandoregel dot be
2023-08-15 19:00 ` [Bug c++/99232] " yagreg7 at gmail dot com
2023-11-24 13:55 ` cvs-commit at gcc dot gnu.org
2024-02-11 13:42 ` nshead at gcc dot gnu.org
2024-02-11 14:07 ` nshead at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).