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

            Bug ID: 124483
           Summary: [[maybe_unused]] is not honoured in a templated class
                    within a C++ module
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sebj_coder3 at pm dot me
  Target Milestone: ---

I think this may be the last bug to allow me to compile my helloworld program
(once https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124466 is resolved)

I have a minimal example this time. In my real program, I have a virtual
function that I want to define as a no-op, but allow client coders to extend,
so I use [[maybe_unused]] on the arguments to the function (it's a callback,
you can see the real one here:
https://github.com/sebsjames/mathplot/blob/dev/modules_gccbz4/mplot/VisualOwnable.h#L2027).

If the class is templated, the [[maybe_unused]] flag is not recognised, so I
get -Werror=unused-parameter.

If the class is NOT templated, [[maybe_unused]] is recognised.

// B.cppm
export module B;

export template <int tp = 1>
struct B
{
    B() {}
    int f() { return tp; }
    virtual void vf ([[maybe_unused]] int b) {}
};

// a.cpp
import B;

int main()
{
    B<1> b;
    return b.f();
}

On g++ 15.2.1 and g++ 16/master I get:

[10:15:28 bgcc16] ninja a -v
[1/6] /opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -DMPLOT_HAVE_STD_FORMAT -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -std=c++20 -E -x c++
/home/seb/src/mathplot/bugrep/a.cpp -MT bugrep/CMakeFiles/a.dir/a.cpp.o.ddi -MD
-MF bugrep/CMakeFiles/a.dir/a.cpp.o.ddi.d -fmodules-ts
-fdeps-file=bugrep/CMakeFiles/a.dir/a.cpp.o.ddi
-fdeps-target=bugrep/CMakeFiles/a.dir/a.cpp.o -fdeps-format=p1689r5 -o
bugrep/CMakeFiles/a.dir/a.cpp.o.ddi.i
[2/6] /opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -DMPLOT_HAVE_STD_FORMAT -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -std=c++20 -E -x c++
/home/seb/src/mathplot/bugrep/B.cppm -MT bugrep/CMakeFiles/a.dir/B.cppm.o.ddi
-MD -MF bugrep/CMakeFiles/a.dir/B.cppm.o.ddi.d -fmodules-ts
-fdeps-file=bugrep/CMakeFiles/a.dir/B.cppm.o.ddi
-fdeps-target=bugrep/CMakeFiles/a.dir/B.cppm.o -fdeps-format=p1689r5 -o
bugrep/CMakeFiles/a.dir/B.cppm.o.ddi.i
[3/6] /usr/local/bin/cmake -E cmake_ninja_dyndep
--tdi=bugrep/CMakeFiles/a.dir/CXXDependInfo.json --lang=CXX --modmapfmt=gcc
--dd=bugrep/CMakeFiles/a.dir/CXX.dd @bugrep/CMakeFiles/a.dir/CXX.dd.rsp
[4/6] /opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -DMPLOT_HAVE_STD_FORMAT -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -std=c++20 -MD -MT
bugrep/CMakeFiles/a.dir/B.cppm.o -MF bugrep/CMakeFiles/a.dir/B.cppm.o.d
-fmodules-ts -fmodule-mapper=bugrep/CMakeFiles/a.dir/B.cppm.o.modmap -MD
-fdeps-format=p1689r5 -x c++ -o bugrep/CMakeFiles/a.dir/B.cppm.o -c
/home/seb/src/mathplot/bugrep/B.cppm
[5/6] /opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -DMPLOT_HAVE_STD_FORMAT -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -std=c++20 -MD -MT
bugrep/CMakeFiles/a.dir/a.cpp.o -MF bugrep/CMakeFiles/a.dir/a.cpp.o.d
-fmodules-ts -fmodule-mapper=bugrep/CMakeFiles/a.dir/a.cpp.o.modmap -MD
-fdeps-format=p1689r5 -x c++ -o bugrep/CMakeFiles/a.dir/a.cpp.o -c
/home/seb/src/mathplot/bugrep/a.cpp
FAILED: bugrep/CMakeFiles/a.dir/a.cpp.o 
/opt/gcc-16/bin/g++  -I/home/seb/src/mathplot -I/usr/include/hdf5/serial
-I/usr/include/freetype2 -I/home/seb/src/mathplot/maths
-I/home/seb/src/mathplot/json/include -g -Wall -Wextra -Wpedantic
-pedantic-errors -Werror -Wfatal-errors -Wno-psabi -O3
-fconstexpr-ops-limit=5000000000 -DMPLOT_HAVE_STD_FORMAT -fopenmp
-DMPLOT_FONTS_DIR="\"/home/seb/src/mathplot/fonts\""  -std=c++20 -MD -MT
bugrep/CMakeFiles/a.dir/a.cpp.o -MF bugrep/CMakeFiles/a.dir/a.cpp.o.d
-fmodules-ts -fmodule-mapper=bugrep/CMakeFiles/a.dir/a.cpp.o.modmap -MD
-fdeps-format=p1689r5 -x c++ -o bugrep/CMakeFiles/a.dir/a.cpp.o -c
/home/seb/src/mathplot/bugrep/a.cpp
In module B, imported at /home/seb/src/mathplot/bugrep/a.cpp:2:
/home/seb/src/mathplot/bugrep/B.cppm: In instantiation of ‘void
B@B<tp>::vf(int) [with int tp = 1]’:
/home/seb/src/mathplot/bugrep/B.cppm:9:18:   required from here
    9 |     virtual void vf ([[maybe_unused]] int b) {}
      |                  ^~
/home/seb/src/mathplot/bugrep/B.cppm:9:43: error: unused parameter ‘b’
[-Werror=unused-parameter]
    9 |     virtual void vf ([[maybe_unused]] int b) {}
      |                      ~~~~~~~~~~~~~~~~~~~~~^
compilation terminated due to -Wfatal-errors.
cc1plus: all warnings being treated as errors
ninja: build stopped: subcommand failed.

Thanks for the work resolving the other bugs I've reported.

Reply via email to